2026-06-10 21:40:08 +02:00
# OpenAI tool calling with history
# Uses a sample function
2026-05-24 13:31:30 +02:00
import yaml
import gradio as gr
import json
import os
from openai import OpenAI
from dotenv import load_dotenv
2026-06-10 21:40:08 +02:00
2026-05-24 13:31:30 +02:00
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 ' ]
2026-06-10 21:40:08 +02:00
SYSTEM_PROMPT = [
{
" role " : " system " ,
" content " : char_config [ ' presets ' ] [ ' default ' ] [ ' system_prompt ' ]
}
]
2026-05-24 13:31:30 +02:00
# Load/save chat history
def load_history ( ) :
if os . path . exists ( HISTORY_FILE ) :
with open ( HISTORY_FILE , " r " ) as f :
return json . load ( f )
2026-06-10 21:40:08 +02:00
return SYSTEM_PROMPT . copy ( )
2026-05-24 13:31:30 +02:00
def save_history ( history ) :
with open ( HISTORY_FILE , " w " ) as f :
json . dump ( history , f , indent = 2 )
def get_riko_response_no_tool ( messages ) :
2026-06-10 21:40:08 +02:00
# Call OpenAI with system prompt + history using the classic Chat API
response = client . chat . completions . create (
2026-05-24 13:31:30 +02:00
model = MODEL ,
2026-06-10 21:40:08 +02:00
messages = messages ,
2026-05-24 13:31:30 +02:00
temperature = 1 ,
top_p = 1 ,
2026-06-10 21:40:08 +02:00
max_tokens = 2048 , # Changed from max_output_tokens
stream = False
2026-05-24 13:31:30 +02:00
)
2026-06-10 21:40:08 +02:00
# The classic API returns the text directly here:
return response . choices [ 0 ] . message . content
2026-05-24 13:31:30 +02:00
def llm_response ( user_input ) :
messages = load_history ( )
2026-06-10 21:40:08 +02:00
# Append user message to memory (clean text format)
2026-05-24 13:31:30 +02:00
messages . append ( {
" role " : " user " ,
2026-06-10 21:40:08 +02:00
" content " : user_input
2026-05-24 13:31:30 +02:00
} )
riko_test_response = get_riko_response_no_tool ( messages )
2026-06-10 21:40:08 +02:00
# append assistant message to regular response
2026-05-24 13:31:30 +02:00
messages . append ( {
2026-06-10 21:40:08 +02:00
" role " : " assistant " ,
" content " : riko_test_response
2026-05-24 13:31:30 +02:00
} )
save_history ( messages )
2026-06-10 21:40:08 +02:00
return riko_test_response
2026-05-24 13:31:30 +02:00
2026-06-10 21:40:08 +02:00
# respond with Long-term memory
2026-05-24 13:31:30 +02:00
def llm_response_with_memory ( user_input , context_memory ) :
messages = load_history ( )
2026-06-10 21:40:08 +02:00
# Safely modify the system prompt (assumes it's always first)
2026-05-24 13:31:30 +02:00
if messages and messages [ 0 ] [ ' role ' ] == ' system ' :
base_content = char_config [ ' presets ' ] [ ' default ' ] [ ' system_prompt ' ]
2026-06-10 21:40:08 +02:00
messages [ 0 ] [ ' content ' ] = f " { base_content } \n \n The 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 } "
2026-05-24 13:31:30 +02:00
else :
# Fallback in case system prompt is missing for some reason
messages . insert ( 0 , {
" role " : " system " ,
2026-06-10 21:40:08 +02:00
" content " : f " { char_config [ ' presets ' ] [ ' default ' ] [ ' system_prompt ' ] } \n \n [Memory] \n { context_memory } "
2026-05-24 13:31:30 +02:00
} )
2026-06-10 21:40:08 +02:00
# Append user message to memory (clean text format)
2026-05-24 13:31:30 +02:00
messages . append ( {
" role " : " user " ,
2026-06-10 21:40:08 +02:00
" content " : user_input
2026-05-24 13:31:30 +02:00
} )
riko_test_response = get_riko_response_no_tool ( messages )
2026-06-10 21:40:08 +02:00
# append assistant message to regular response
2026-05-24 13:31:30 +02:00
messages . append ( {
2026-06-10 21:40:08 +02:00
" role " : " assistant " ,
" content " : riko_test_response
2026-05-24 13:31:30 +02:00
} )
save_history ( messages )
2026-06-10 21:40:08 +02:00
return riko_test_response
2026-05-24 13:31:30 +02:00
if __name__ == " __main__ " :
print ( ' running main ' )
2026-06-10 21:40:08 +02:00
# 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 )