Spaces:
Running
Running
File size: 2,071 Bytes
28d024b f951332 28d024b f951332 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import cohere
import asyncio
def send_message_stream(system_message, user_message, conversation_history, api_key, model_name="command-a-03-2025"):
"""Stream response from Cohere API"""
# 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 streaming request to Cohere
stream = co.chat_stream(
model=model_name,
messages=messages
)
# Collect full response for history
full_response = ""
# Yield chunks as they come
for chunk in stream:
if chunk.type == "content-delta":
text_chunk = chunk.delta.message.content.text
full_response += text_chunk
yield text_chunk
# Update conversation history after streaming is complete
conversation_history.append({"role": "user", "content": user_message})
conversation_history.append({"role": "assistant", "content": full_response})
def send_message(system_message, user_message, conversation_history, api_key, model_name="command-a-03-2025"):
"""Non-streaming version for backward compatibility"""
# 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 synchronously
response = co.chat(
model=model_name,
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 |