
    ̄Ii                     2   d dl mZmZmZmZmZmZmZ ddlm	Z	 d dl
Z
d dlZd dlZddlmZ  e
j                  e      Z ede      Z e	       Zej)                  d      d        Zej)                  d	d
g      d        Zej)                  dd
g      d        Zy)    )	Blueprintrender_templaterequestjsonifycurrent_appResponsestream_with_context   )ChatServiceN)Config
playgroundz/playgroundc                      t        d      S )Nz	home.html)r        ;/srv/www/ollama/chat-ollama/src/routes/playground_routes.pyr   r      s    ;''r   z/api/playground/generatePOST)methodsc                     	 t         j                  d       t        j                         } | s$t         j	                  d       t        ddi      dfS | j                  d      }| j                  dd      }| j                  d	d
      }| j                  dd      }| j                  dd      }| j                  dd      }|s$t         j	                  d       t        ddi      dfS t         j                  d|        t         j                  d|        t         j                  d|        t         j                  d|        t         j                  d|        t         j                  d|        	 t        j                  |||d      }|rd|vr$t         j	                  d       t        ddi      dfS t        d|d   i      S # t        $ rC}t         j	                  dt        |              t        dt        |      i      dfcY d }~S d }~ww xY w# t        $ rC}t         j	                  d t        |              t        dt        |      i      dfcY d }~S d }~ww xY w)!Nz,Received request to /api/playground/generatezNo data provided in requesterrorzNo data provided  messagemodelzllama3:latestsystem_promptzYou are a helpful AI assistant.temperaturegffffff?
max_tokensi  top_pg?zNo message provided in requestzNo message providedzGenerating response for model: z	Message: zSystem prompt: zTemperature: zMax tokens: zTop P: F)r   r   r   streamresponsez"Invalid response from chat servicez Invalid response from AI service  zError from chat service: zError generating response: )loggerinfor   get_jsonr   r   getdebugchat_servicesend_message	Exceptionstr)	datar   r   r   r   r   r   resultes	            r   generater,      s-   -/BC!LL67G%7893>>((9%/22SThh}c2XXlD1
#&LL9:G%:;<cAA5eW=>y	*+}o67}[M23|J<01wug&'	3!..+	 / F Zv5AB)KLMsRRJz(:;<< 	3LL4SVH=>GSV,-s22	3  /23q6(;<Q()3../s]   AH BH "BH 3AG 6G 	H8HHH HH 	I!8II!I!z/api/playground/uploadc                     	 dt         j                  vrt        ddi      dfS t         j                  d   } | j                  dk(  rt        ddi      dfS | st        ddi      dfS | j	                         j                  d      }t        d	|d
      S # t        $ rC}t        j                  dt        |              t        dt        |      i      dfcY d }~S d }~ww xY w)Nfiler   zNo file providedr    zNo file selectedzInvalid filezutf-8zFile uploaded successfully)r   contentzError uploading file: r   )
r   filesr   filenamereaddecoder'   r    r   r(   )r.   r0   r+   s      r   upload_filer5   A   s    /&G%7893>>}}V$==BG%7893>>G^45s::))+$$W-3
  	
  /-c!fX67Q()3../s.    B 0B B %,B 	C8CCC)flaskr   r   r   r   r   r   r	   services.chatr   loggingrequestsjsonconfigr   	getLogger__name__r    playground_bpr%   router   r,   r5   r   r   r   <module>r@      s    j j j '    			8	$,1}]#( $( /&B./ C./` -x@/ A/r   