import datetime import json import random import streamlit as st import openai def get_prompt(): 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)) ) prompt = f"""Sono amnesico, ecco alcune informazioni su di me: '{context}' Ora mi dimenticherò di quanto sopra, e ti farò delle domande, e voglio che tu mi fornisca le risposte, in maniera concisa, devi darmi del tu. Domanda:""" return prompt def get_answer(input): prompt = get_prompt() openai.api_key = st.secrets("OPENAI_API_KEY") response = openai.Completion.create( model="text-davinci-003", prompt=prompt + input, temperature=0, max_tokens=520, top_p=1.0, frequency_penalty=0.2, presence_penalty=0.0, ) ans = response["choices"][0]["text"].replace("Risposta:", "").strip() print(f"{input} {ans}") return ans