-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
155 lines (129 loc) · 4.61 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import openai, io, tiktoken
from fastapi import FastAPI, HTTPException, File
from pydantic_settings import BaseSettings
from fastapi.middleware.cors import CORSMiddleware
# from google.cloud import texttospeech
class Settings(BaseSettings):
# OpenAI API Settings
openai_api_key: str = "secret"
chatcompletion_model: str = "gpt-3.5-turbo"
whisper_model: str = "whisper-1"
chatcompletion_temperature: float = 0.5
max_response_tokens: int = 1000
token_limit: int = 4096
context_depth: int = 4096
# Application Settings
fastapi_host: str = "0.0.0.0"
fastapi_port: int = 8000
# Loading .env file if present
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
class NamedBytesIO(io.BytesIO):
def __init__(self, buffer, name=None):
super().__init__(buffer)
self.name = name
settings = Settings()
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# OpenAI API key setup
openai.api_key = settings.openai_api_key
def countToken(str):
encoding = tiktoken.encoding_for_model(settings.chatcompletion_model)
encoded_text = encoding.encode(str)
return len(encoded_text)
messages = [
{
"role": "system",
"content": """
Nama kamu adalah Stella. Kamu adalah ahli dokter kesehatan dan nutrisi. Kamu akan menerima pertanyaan menggunakan bahasa gaul pertanyaan dari pasien.
""",
}
]
token_data = []
async def chatGptResponse(message):
try:
messages.append({"role": "user", "content": message})
token_data.append(countToken(message))
response = openai.ChatCompletion.create(
model=settings.chatcompletion_model,
messages=messages,
temperature=settings.chatcompletion_temperature,
max_tokens=settings.max_response_tokens,
)
messages.append(
{
"role": "assistant",
"content": response["choices"][0]["message"]["content"],
},
)
total_tokens = response["usage"]["total_tokens"]
# while 16000 - total_tokens < 2000:
# messages.pop(1)
# removed_token = token_data.pop(0)
# total_tokens -= removed_token
while total_tokens > settings.context_depth:
messages.pop(1)
removed_token = token_data.pop(0)
total_tokens -= removed_token
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
return response
async def checkModeration(message):
try:
moderation_response = openai.Moderation.create(input=message)
except:
raise HTTPException(status_code=500, detail=str(e))
return moderation_response["results"][0]["flagged"]
@app.post("/transcribe")
async def transcribe(audio_file: bytes = File(...)):
try:
# Get audio transcription
audio_buffer = NamedBytesIO(audio_file, name="audio.wav")
# transcribe audio to text
transcript = openai.Audio.transcribe(settings.whisper_model, audio_buffer)
user_message = transcript["text"]
# Check moderation
flagged = await checkModeration(user_message)
if not flagged:
# Get chatgpt completion
completion = await chatGptResponse(user_message)
content = {
"message": messages,
"prompt": transcript["text"],
"completion": completion["choices"][0]["message"]["content"],
"total_tokens": completion["usage"]["total_tokens"],
}
else:
content = {
"completion": "Maaf, pertanyaan atau statement kamu melanggar Moderation Policy kami",
}
return content
except Exception as e:
print(e)
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
@app.get("/reset")
async def reset_context():
try:
global messages, token_data
messages = [
{
"role": "system",
"content": """
Nama kamu adalah Stella. Kamu adalah ahli dokter kesehatan dan nutrisi. Kamu akan menerima pertanyaan atau tanggapan dari pasien.
kamu harus menjawab dengan singkat, padat, jelas menggunakan bahasa gaul anak jaksel.
""",
}
]
token_data = []
return {"status": "success", "message": "Context has been reset."}
except Exception as e:
return {"status": "error", "message": str(e)}