-
Notifications
You must be signed in to change notification settings - Fork 0
/
oai.py
61 lines (51 loc) · 1.9 KB
/
oai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""OpenAI API connector."""
# Import from standard library
import os
import logging
# Import from 3rd party libraries
import openai
import streamlit as st
# Assign credentials from environment variable or streamlit secrets dict
openai.api_key = '[API key]'
# Suppress openai request/response logging
# Handle by manually changing the respective APIRequestor methods in the openai package
# Does not work hosted on Streamlit since all packages are re-installed by Poetry
# Alternatively (affects all messages from this logger):
logging.getLogger("openai").setLevel(logging.WARNING)
class Openai:
"""OpenAI Connector."""
@staticmethod
def moderate(prompt: str) -> bool:
"""Call OpenAI GPT Moderation with text prompt.
Args:
prompt: text prompt
Return: boolean if flagged
"""
try:
response = openai.Moderation.create(prompt)
return response["results"][0]["flagged"]
except Exception as e:
logging.error(f"OpenAI API error: {e}")
st.session_state.text_error = f"OpenAI API error: {e}"
@staticmethod
def complete(prompt: str, temperature: float = 0.9, max_tokens: int = 50) -> str:
"""Call OpenAI GPT Completion with text prompt.
Args:
prompt: text prompt
Return: predicted response text
"""
kwargs = {
"engine": "text-davinci-003",
"prompt": prompt,
"temperature": temperature,
"max_tokens": max_tokens,
"top_p": 1, # default
"frequency_penalty": 0, # default,
"presence_penalty": 0, # default
}
try:
response = openai.Completion.create(**kwargs)
return response["choices"][0]["text"]
except Exception as e:
logging.error(f"OpenAI API error: {e}")
st.session_state.text_error = f"OpenAI API error: {e}"