1+ from __future__ import annotations
2+
3+ import base64
14import glob
25import os
36import subprocess
3134)
3235
3336
34- def _build_messages (content : str , system_prompt : Optional [str ] = None ) -> list :
37+ def _build_messages (content : str , system_prompt : Optional [str ] = None , image_files : Optional [ list ] = None ) -> list :
3538 """Build messages list with optional system prompt."""
3639 messages = []
3740 if system_prompt :
3841 messages .append ({'role' : 'system' , 'content' : system_prompt })
3942 messages .append ({'role' : 'user' , 'content' : content })
43+
44+ if image_files :
45+ messages [- 1 ]["images" ] = [base64 .b64encode (image_file .read ()).decode ("utf-8" ) for image_file in image_files ]
4046 return messages
4147
4248
4349def chat_with_llama_server_http (
44- model : str , content : str , system_prompt : Optional [str ] = None , timeout : int = 300
50+ model : str ,
51+ content : str ,
52+ system_prompt : Optional [str ] = None ,
53+ timeout : int = 300 ,
54+ image_files : Optional [list ] = None ,
4555) -> str :
4656 """Handle chat using llama-server HTTP API."""
4757 if not LLAMA_SERVER_URL :
4858 raise Exception ("LLAMA_SERVER_URL environment variable not set" )
4959
5060 try :
51- messages = _build_messages (content , system_prompt )
61+ messages = _build_messages (content , system_prompt , image_files = []) # TODO: Pass image files
5262
5363 response = requests .post (
5464 f'{ LLAMA_SERVER_URL } /v1/chat/completions' ,
@@ -84,15 +94,23 @@ def is_llamacpp_available(model: str) -> bool:
8494 return resolve_model_path (model ) is not None
8595
8696
87- def chat_with_ollama (model : str , content : str , system_prompt : Optional [str ] = None ) -> str :
97+ def chat_with_ollama (
98+ model : str , content : str , system_prompt : Optional [str ] = None , image_files : Optional [list ] = None
99+ ) -> str :
88100 """Handle chat using ollama."""
89- messages = _build_messages (content , system_prompt )
101+ messages = _build_messages (content , system_prompt , image_files )
90102
91103 response = ollama .chat (model = model , messages = messages , stream = False )
92104 return response .message .content
93105
94106
95- def chat_with_llamacpp (model : str , content : str , system_prompt : Optional [str ] = None , timeout : int = 300 ) -> str :
107+ def chat_with_llamacpp (
108+ model : str ,
109+ content : str ,
110+ system_prompt : Optional [str ] = None ,
111+ timeout : int = 300 ,
112+ image_files : Optional [list ] = None ,
113+ ) -> str :
96114 """Handle chat using llama.cpp CLI."""
97115 model_path = resolve_model_path (model )
98116
@@ -105,6 +123,9 @@ def chat_with_llamacpp(model: str, content: str, system_prompt: Optional[str] =
105123 if system_prompt :
106124 cmd .extend (['--system-prompt' , system_prompt ])
107125
126+ if image_files :
127+ pass # TODO: pass image files
128+
108129 try :
109130 result = subprocess .run (cmd , capture_output = True , text = False , timeout = timeout , check = True )
110131
@@ -125,20 +146,26 @@ def chat_with_llamacpp(model: str, content: str, system_prompt: Optional[str] =
125146 raise Exception ("Llama.cpp CLI not found" )
126147
127148
128- def chat_with_model (model : str , content : str , llama_mode : str = "cli" , system_prompt : Optional [str ] = None ) -> str :
149+ def chat_with_model (
150+ model : str ,
151+ content : str ,
152+ llama_mode : str = "cli" ,
153+ system_prompt : Optional [str ] = None ,
154+ image_files : Optional [list ] = None ,
155+ ) -> str :
129156 """Route chat request based on llama_mode: server (external), cli, or ollama fallback; and with optional system prompt."""
130157 if is_llamacpp_available (model ):
131158 if llama_mode == "server" :
132159 if not LLAMA_SERVER_URL :
133160 raise Exception ("LLAMA_SERVER_URL environment variable not set for server mode" )
134- return chat_with_llama_server_http (model , content , system_prompt )
161+ return chat_with_llama_server_http (model , content , system_prompt , image_files )
135162 elif llama_mode == "cli" :
136- return chat_with_llamacpp (model , content , system_prompt )
163+ return chat_with_llamacpp (model , content , system_prompt , image_files )
137164 else :
138165 raise ValueError (f"Invalid llama_mode: '{ llama_mode } '. Valid options are 'server' or 'cli'." )
139166 else :
140167 # Model not available in llama.cpp, use ollama
141- return chat_with_ollama (model , content , system_prompt )
168+ return chat_with_ollama (model , content , system_prompt , image_files )
142169
143170
144171def authenticate () -> str :
@@ -158,16 +185,16 @@ def authenticate() -> str:
158185def chat ():
159186 """Handle chat request with optional llama_mode and system prompt parameters."""
160187 authenticate ()
161- params = request .get_json ( )
162- model = params . get ('model ' , DEFAULT_MODEL )
163- content = params . get ('content ' , '' )
164- llama_mode = params . get ('llama_mode' , 'cli ' )
165- system_prompt = params . get ( 'system_prompt' )
188+ model = request .form . get ( 'model' , DEFAULT_MODEL )
189+ content = request . form . get ('content ' , '' )
190+ llama_mode = request . form . get ('llama_mode ' , 'cli ' )
191+ system_prompt = request . form . get ('system_prompt ' )
192+ image_files = list ( request . files . values () )
166193
167194 if not content .strip ():
168195 abort (400 , description = 'Missing prompt content' )
169196
170- response_content = chat_with_model (model , content , llama_mode , system_prompt )
197+ response_content = chat_with_model (model , content , llama_mode , system_prompt , image_files )
171198 return jsonify (response_content )
172199
173200
0 commit comments