111 lines
3.2 KiB
Python
111 lines
3.2 KiB
Python
# OpenAI tool calling with history
|
|
# Uses a sample function
|
|
import yaml
|
|
import gradio as gr
|
|
import json
|
|
import os
|
|
from openai import OpenAI
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
with open('character_config.yaml', 'r') as f:
|
|
char_config = yaml.safe_load(f)
|
|
|
|
client = OpenAI(api_key=openai_api_key)
|
|
|
|
# Constants
|
|
HISTORY_FILE = char_config['history_file']
|
|
MODEL = char_config['model']
|
|
|
|
SYSTEM_PROMPT = [
|
|
{
|
|
"role": "system",
|
|
"content": char_config['presets']['default']['system_prompt']
|
|
}
|
|
]
|
|
|
|
# Load/save chat history
|
|
def load_history():
|
|
if os.path.exists(HISTORY_FILE):
|
|
with open(HISTORY_FILE, "r") as f:
|
|
return json.load(f)
|
|
return SYSTEM_PROMPT.copy()
|
|
|
|
def save_history(history):
|
|
with open(HISTORY_FILE, "w") as f:
|
|
json.dump(history, f, indent=2)
|
|
|
|
def get_riko_response_no_tool(messages):
|
|
# Call OpenAI with system prompt + history using the classic Chat API
|
|
response = client.chat.completions.create(
|
|
model=MODEL,
|
|
messages=messages,
|
|
temperature=1,
|
|
top_p=1,
|
|
max_tokens=2048, # Changed from max_output_tokens
|
|
stream=False
|
|
)
|
|
# The classic API returns the text directly here:
|
|
return response.choices[0].message.content
|
|
|
|
def llm_response(user_input):
|
|
messages = load_history()
|
|
|
|
# Append user message to memory (clean text format)
|
|
messages.append({
|
|
"role": "user",
|
|
"content": user_input
|
|
})
|
|
|
|
riko_test_response = get_riko_response_no_tool(messages)
|
|
|
|
# append assistant message to regular response
|
|
messages.append({
|
|
"role": "assistant",
|
|
"content": riko_test_response
|
|
})
|
|
|
|
save_history(messages)
|
|
return riko_test_response
|
|
|
|
# respond with Long-term memory
|
|
def llm_response_with_memory(user_input, context_memory):
|
|
messages = load_history()
|
|
|
|
# Safely modify the system prompt (assumes it's always first)
|
|
if messages and messages[0]['role'] == 'system':
|
|
base_content = char_config['presets']['default']['system_prompt']
|
|
messages[0]['content'] = f"{base_content}\n\nThe following memories may or may not be relevent information from past conversations. If it is not relevent to this conversation, ignore it:\n{context_memory}"
|
|
else:
|
|
# Fallback in case system prompt is missing for some reason
|
|
messages.insert(0, {
|
|
"role": "system",
|
|
"content": f"{char_config['presets']['default']['system_prompt']}\n\n[Memory]\n{context_memory}"
|
|
})
|
|
|
|
# Append user message to memory (clean text format)
|
|
messages.append({
|
|
"role": "user",
|
|
"content": user_input
|
|
})
|
|
|
|
riko_test_response = get_riko_response_no_tool(messages)
|
|
|
|
# append assistant message to regular response
|
|
messages.append({
|
|
"role": "assistant",
|
|
"content": riko_test_response
|
|
})
|
|
|
|
save_history(messages)
|
|
return riko_test_response
|
|
|
|
if __name__ == "__main__":
|
|
print('running main')
|
|
# Fixed the test call so it passes a valid message array instead of a raw string
|
|
test_messages = [{"role": "user", "content": "hi riko"}]
|
|
response = get_riko_response_no_tool(test_messages)
|
|
print(response)
|