Skip to content

Instantly share code, notes, and snippets.

@rsantana74
Last active February 22, 2023 04:07
Show Gist options
  • Save rsantana74/f97784de876d0320cae817a37fce350c to your computer and use it in GitHub Desktop.
Save rsantana74/f97784de876d0320cae817a37fce350c to your computer and use it in GitHub Desktop.
Open AI Q&A bot.
import openai
import os
import csv
openai.api_key = os.environ["OPENAI_API_KEY"]
prompt = "I am a highly intelligent question answering bot. If you ask me a question that is rooted in truth, I will give you the answer. If you ask me a question that is nonsense, trickery, or has no clear answer, I will respond with \"Unknown\".\n\n"
with open('cfqa.csv') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
prompt += f"Q: {row['question']}\nA: {row['answer']}\n\n"
while True:
question = input('Q: ')
if question.strip() in ['exit', 'quit']:
break
promptWithQuestion = f"{prompt}\nQ: {question}\nA: "
response = openai.Completion.create(
model="text-davinci-003",
prompt=promptWithQuestion,
max_tokens=100,
temperature=0.5,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\n"],
)
answer = response.choices[0].text.strip()
if answer.lower().strip(". ") in ["", "unknown"]:
answer = "Desculpe mas não entendi. Por favor, refaça a pergunta de outra forma."
else:
prompt += f"Q: {question}\nA: {answer}\n\n"
print(answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment