Command_RTC / cohereAPI.py
RSHVR's picture
Create cohereAPI.py
28d024b verified
raw
history blame
1.13 kB
import cohere
import asyncio
async def send_message(system_message, user_message, conversation_history, api_key):
# Initialize the Cohere client
co = cohere.ClientV2(api_key)
# Prepare all messages including history
messages = [{"role": "system", "content": system_message}]
messages.extend(conversation_history)
messages.append({"role": "user", "content": user_message})
# Send request to Cohere asynchronously
# Since the Cohere Python client may not have native async support,
# we'll run it in a thread pool to avoid blocking
loop = asyncio.get_event_loop()
response = await loop.run_in_executor(
None,
lambda: co.chat(
model="command-a-03-2025",
messages=messages
)
)
# Get the response
response_content = response.message.content[0].text
# Update conversation history for this session
conversation_history.append({"role": "user", "content": user_message})
conversation_history.append({"role": "assistant", "content": response_content})
return response_content, conversation_history