Spaces:
Build error
Build error
File size: 1,133 Bytes
28d024b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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 |