sonIA / utils.py
davidefiocco's picture
Move to chatGPT to lower costs
53e30d5
raw
history blame
1.46 kB
import datetime
import json
import random
import streamlit as st
import openai
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"""Il mio nome utente è Giuseppe, ed ecco alcune informazioni su di me: '{context}' .
Di seguito, ti farò alcune domande di cui non ricordo la risposta, perché sono amnesico, e tu dovrai rispondermi in modo conciso.
"""
return system
def get_answer(input):
system_content = get_system()
openai.api_key = st.secrets["OPENAI_API_KEY"]
response = openai.ChatCompletion.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