chore(backend): remove deprecated inference engines and character agent
This commit is contained in:
1 parent
13e89b63fc
commit
f3a8376df6
3 files changed
-509
No files matched your search
@@ -2,7 +2,6 @@ import threading
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
#import ollama
|
||||
import traceback
|
||||
import random
|
||||
from stream import Stream
|
||||
@@ -163,16 +162,6 @@ async def chat_completion(app,
|
||||
|
||||
if model_engine == "llama-cpp":
|
||||
result = await local_chat_completion(app, messages, model=model_name, tools=tools, opts=opts, agent_run=agent_run)
|
||||
elif model_engine == "replicate":
|
||||
# Clone opts to avoid mutating the caller's dict
|
||||
opts = dict(opts)
|
||||
if 'parameters' in db_model:
|
||||
opts['parameters'] = db_model['parameters']
|
||||
if 'format' in db_model:
|
||||
opts['format'] = db_model['format']
|
||||
result = await replicate_completion(app, messages, model=model_name, tools=tools, opts=opts, agent_run=agent_run)
|
||||
elif model_engine == "ollama":
|
||||
result = await ollama_chat_completion(app, messages, model=model_name, tools=tools, opts=opts, agent_run=agent_run)
|
||||
elif model_engine == "openrouter":
|
||||
result = await openrouter_chat_completion(app, messages, model=model_name, tools=tools, opts=opts, agent_run=agent_run)
|
||||
else:
|
||||
@@ -256,57 +245,6 @@ async def local_chat_completion(app, messages, model, tools=[], opts={}, agent_r
|
||||
return None
|
||||
return content_stream
|
||||
|
||||
async def ollama_list(app, messages, model, opts={}):
|
||||
# TODO: test the output, and use this
|
||||
return ollama.list()
|
||||
|
||||
async def ollama_chat_completion(app, messages, model, tools=[], opts={}, agent_run=None):
|
||||
raise Exception("ollama disabled")
|
||||
if agent_run:
|
||||
content_stream = agent_run.stream
|
||||
else:
|
||||
content_stream = Stream(app)
|
||||
|
||||
async def worker():
|
||||
print("START OLLAMA INFERENCE")
|
||||
kwargs = {
|
||||
'model': model,
|
||||
'messages': [msg.to_dict() for msg in messages],
|
||||
'stream': True,
|
||||
}
|
||||
|
||||
# Pass tools in Ollama format if provided
|
||||
formatted_tools = format_tools_for_engine(tools, "ollama") if tools else None
|
||||
if formatted_tools:
|
||||
kwargs['tools'] = formatted_tools
|
||||
|
||||
try:
|
||||
stream_response = await ollama.AsyncClient().chat(**kwargs)
|
||||
|
||||
async for part in stream_response:
|
||||
if agent_run and agent_run.cancelled:
|
||||
print(f"Cancelling ollama inference for run {agent_run.id}")
|
||||
break
|
||||
|
||||
if part.message.content:
|
||||
content_stream.feed(part.message.content)
|
||||
except asyncio.CancelledError:
|
||||
print(f"Ollama task cancelled for run {agent_run.id}")
|
||||
raise
|
||||
|
||||
content_stream.close()
|
||||
print("END OLLAMA INFERENCE")
|
||||
|
||||
task = asyncio.create_task(worker())
|
||||
if agent_run:
|
||||
agent_run.task = task
|
||||
else:
|
||||
task.start()
|
||||
|
||||
if agent_run:
|
||||
return None
|
||||
return content_stream
|
||||
|
||||
|
||||
async def openrouter_chat_completion(app, messages, model, tools=[], opts={}, agent_run=None):
|
||||
if not agent_run:
|
||||
@@ -437,56 +375,6 @@ async def openrouter_chat_completion(app, messages, model, tools=[], opts={}, ag
|
||||
agent_run.task = task
|
||||
|
||||
|
||||
async def replicate_completion(app, messages, model=None, tools=[], opts={}, agent_run=None):
|
||||
if model is None:
|
||||
model = Models.llama_8b
|
||||
|
||||
parameter_names = opts.get('parameters', {})
|
||||
|
||||
arguments = {}
|
||||
|
||||
# Build the prompt, injecting tool descriptions for models that rely on
|
||||
# prompt-based tool usage rather than native function calling.
|
||||
prompt_text = format_instruct(messages)
|
||||
if tools:
|
||||
tool_prefix = build_tool_prompt_prefix(tools)
|
||||
if tool_prefix:
|
||||
prompt_text = tool_prefix + "\n\n" + prompt_text
|
||||
|
||||
arguments[parameter_names.get('prompt', 'prompt')] = prompt_text
|
||||
arguments[parameter_names.get('temperature', 'temperature')] = opts.get('temperature') or 0.75
|
||||
arguments[parameter_names.get('max_tokens', 'max_tokens')] = opts.get('max_tokens', 2048)
|
||||
|
||||
arguments[parameter_names.get('system_prompt', 'system_prompt')] = ""
|
||||
|
||||
result = await replicate_base_completion(app, model, arguments, agent_run=agent_run)
|
||||
if agent_run:
|
||||
return None
|
||||
return result
|
||||
|
||||
async def replicate_base_completion(app, model, input, agent_run=None):
|
||||
if agent_run:
|
||||
content_stream = agent_run.stream
|
||||
else:
|
||||
content_stream = Stream(app)
|
||||
thread = threading.Thread(
|
||||
target=run_replicate_inference_loop,
|
||||
kwargs={
|
||||
'replicate_client': app.replicate_client,
|
||||
'model': model,
|
||||
'content_stream': content_stream,
|
||||
'input': input,
|
||||
'agent_run': agent_run
|
||||
}
|
||||
)
|
||||
thread.start()
|
||||
#response = await content_stream.content_wait()
|
||||
#return response.lstrip()
|
||||
if agent_run:
|
||||
agent_run.thread = thread
|
||||
return None
|
||||
return content_stream
|
||||
|
||||
async def mock_completion(app, agent_run=None):
|
||||
VERBS = ['fix','scan','sync','deploy','parse','render','cache','trace',
|
||||
'merge','purge','index','probe','flush','bind','warm','drain']
|
||||
@@ -512,25 +400,3 @@ async def mock_completion(app, agent_run=None):
|
||||
|
||||
asyncio.create_task(content_task())
|
||||
if not agent_run: return content_stream
|
||||
|
||||
|
||||
def run_replicate_inference_loop(replicate_client, model, input, content_stream, agent_run=None):
|
||||
print("START INFERENCE")
|
||||
completion = replicate_client.run(model, input=input, stream=True)
|
||||
for event in completion:
|
||||
if agent_run and agent_run.cancelled:
|
||||
print(f"Cancelling replicate inference for run {agent_run.id}")
|
||||
break
|
||||
# TODO: does completion need explicit cancellation?
|
||||
|
||||
if event:
|
||||
content_stream.feed(str(event))
|
||||
print("END INFERENCE")
|
||||
content_stream.close()
|
||||
|
||||
def format_instruct(messages):
|
||||
prompt = "<|begin_of_text|>"
|
||||
for message in messages:
|
||||
prompt += f"<|start_header_id|>{message.role}<|end_header_id|>\n\n{message.content}<|eot_id|>"
|
||||
prompt += f"<|start_header_id|>assistant<|end_header_id|>"
|
||||
return prompt
|
||||
Reference in new issue
Block a user