sonIA / utils.py
Davide Fiocco
update openai and streamlit_authenticator compatibility
0068562
import datetime
import json
import random
import streamlit as st
from openai import OpenAI
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def get_system():
BIRTHYEAR = 1952
BIRTHMONTH = 5
OTHERBIRTHYEAR = 1984
now = datetime.datetime.now()
with open("context.json") as f:
context = (
json.load(f)["info"]
.replace("[YEAR]", str(now.year))
.replace("[TODAY]", f"{now:%d-%m-%Y}")
.replace("[BIRTHYEAR]", str(BIRTHYEAR))
.replace(
"[AGE]",
str((now - datetime.datetime(BIRTHYEAR, BIRTHMONTH, 1)).days // 365),
)
.replace("[OTHERAGE]", str(now.year - OTHERBIRTHYEAR))
)
system = f"""Giuseppe è amnesico, ecco alcune informazioni su di lui: '{context}' .
Di seguito, Giuseppe farà domande di cui non sa o non ricorda la risposta. Cerca di aiutarlo con risposte concise, in un italiano perfetto.
"""
return system
def get_answer(input):
system_content = get_system()
response = client.chat.completions.create(model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": system_content},
{"role": "user", "content": input},
])
ans = response.choices[0].message.content
logger.info(f"Q: {input} - A: {ans}")
return ans