Fixes for anthropic
This commit is contained in:
+17
-11
@@ -113,7 +113,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|||||||
conversation_history[user_id].append({"role": "user", "content": user_message})
|
conversation_history[user_id].append({"role": "user", "content": user_message})
|
||||||
|
|
||||||
# Prepare messages for OpenAI API
|
# 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)
|
response = get_chat_response(messages)
|
||||||
|
|
||||||
@@ -163,17 +163,13 @@ def call_tool(function_call):
|
|||||||
return tool.execute(function_name, **eval(function_args))
|
return tool.execute(function_name, **eval(function_args))
|
||||||
|
|
||||||
def get_chat_response(messages):
|
def get_chat_response(messages):
|
||||||
if use_anthropic:
|
return get_claude_response(messages) if use_anthropic else get_openai_response(messages)
|
||||||
response = get_openai_response(messages)
|
|
||||||
else:
|
|
||||||
response = get_claude_response(messages)
|
|
||||||
return response
|
|
||||||
|
|
||||||
def get_openai_response(messages):
|
def get_openai_response(messages):
|
||||||
model = GPT_4O if use_smart_model else GPT_4O_MINI
|
model = GPT_4O if use_smart_model else GPT_4O_MINI
|
||||||
response = openai_client.chat.completions.create(
|
response = openai_client.chat.completions.create(
|
||||||
model=model,
|
model=model,
|
||||||
messages=messages,
|
messages = [{"role": "system", "content": system_prompt}] + messages,
|
||||||
functions=functions,
|
functions=functions,
|
||||||
function_call="auto",
|
function_call="auto",
|
||||||
max_tokens=model_max_tokens[model]
|
max_tokens=model_max_tokens[model]
|
||||||
@@ -181,25 +177,35 @@ def get_openai_response(messages):
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
def get_claude_response(messages):
|
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(
|
response = anthropic_client.messages.create(
|
||||||
|
system=system_prompt,
|
||||||
messages=messages,
|
messages=messages,
|
||||||
|
tools=anthropic_tools,
|
||||||
max_tokens=4096,
|
max_tokens=4096,
|
||||||
model="claude-3-5-sonnet-20240620"
|
model="claude-3-5-sonnet-20240620"
|
||||||
)
|
)
|
||||||
return response
|
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
|
global use_smart_model
|
||||||
use_smart_model = not use_smart_model
|
use_smart_model = not use_smart_model
|
||||||
model = GPT_4O if use_smart_model else GPT_4O_MINI
|
model = GPT_4O if use_smart_model else GPT_4O_MINI
|
||||||
logging.info(f"Switched to model: {model}")
|
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
|
global use_anthropic
|
||||||
use_anthropic = not use_anthropic
|
use_anthropic = not use_anthropic
|
||||||
logging.info("Using Anthropic" if use_anthropic else "Using OpenAI")
|
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:
|
async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
if use_anthropic:
|
if use_anthropic:
|
||||||
|
|||||||
Reference in New Issue
Block a user