|
|
import cohere |
|
|
import os |
|
|
from dotenv import load_dotenv |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
API_KEY = os.getenv("SECRET_API_KEY") |
|
|
if API_KEY is None: |
|
|
raise RuntimeError("SECRET_API_KEY not set in environment") |
|
|
|
|
|
|
|
|
model = cohere.ClientV2(API_KEY) |
|
|
|
|
|
|
|
|
def get_response(query, context=""): |
|
|
messages = [{ |
|
|
"role": "system", |
|
|
"content": ( |
|
|
"You are an AI assistant. Use the context provided by the user to give the user a concise answer to their prompt. " |
|
|
"If the answer isn't present do not make it up, rather, inform the user that you do not know the answer" |
|
|
) |
|
|
}] |
|
|
if context: |
|
|
messages.append({"role": "system", "content": context}) |
|
|
messages.append({"role": "user", "content": query}) |
|
|
|
|
|
response = model.chat( |
|
|
model="command-a-03-2025", |
|
|
messages=messages |
|
|
) |
|
|
return response.message.content[0].text.strip() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get_response("How are you?", "Just reply") |