Fixes for anthropic

This commit is contained in:
2024-08-18 08:55:22 -05:00
parent 5134312cab
commit 3d521f4790
3 changed files with 17 additions and 11 deletions
+17 -11
View File
@@ -113,7 +113,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
conversation_history[user_id].append({"role": "user", "content": user_message})
# Prepare messages for OpenAI API
messages = [{"role": "system", "content": system_prompt}] + conversation_history[user_id]
messages = conversation_history[user_id]
response = get_chat_response(messages)
@@ -163,17 +163,13 @@ def call_tool(function_call):
return tool.execute(function_name, **eval(function_args))
def get_chat_response(messages):
if use_anthropic:
response = get_openai_response(messages)
else:
response = get_claude_response(messages)
return response
return get_claude_response(messages) if use_anthropic else get_openai_response(messages)
def get_openai_response(messages):
model = GPT_4O if use_smart_model else GPT_4O_MINI
response = openai_client.chat.completions.create(
model=model,
messages=messages,
messages = [{"role": "system", "content": system_prompt}] + messages,
functions=functions,
function_call="auto",
max_tokens=model_max_tokens[model]
@@ -181,25 +177,35 @@ def get_openai_response(messages):
return response
def get_claude_response(messages):
anthropic_tools = [
{
"name": function['name'],
"description": function['description'],
"input_schema": function['parameters'] if function['parameters'] not in [None, {}] else {"type": "object", "properties": {"param1": {"type": "string", "description": "Unnecessary"}}, "required": []}
}
for function in functions
]
response = anthropic_client.messages.create(
system=system_prompt,
messages=messages,
tools=anthropic_tools,
max_tokens=4096,
model="claude-3-5-sonnet-20240620"
)
return response
def switch(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
async def switch(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
global use_smart_model
use_smart_model = not use_smart_model
model = GPT_4O if use_smart_model else GPT_4O_MINI
logging.info(f"Switched to model: {model}")
update.message.reply_text(f"Switched to model: {model}")
await update.message.reply_text(f"Switched to model: {model}")
def switch_anthropic(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
async def switch_anthropic(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
global use_anthropic
use_anthropic = not use_anthropic
logging.info("Using Anthropic" if use_anthropic else "Using OpenAI")
update.message.reply_text("Using Anthropic" if use_anthropic else "Using OpenAI")
await update.message.reply_text("Using Anthropic" if use_anthropic else "Using OpenAI")
async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if use_anthropic: