-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
103 lines (81 loc) · 2.63 KB
/
server.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
import json
import logging
import openai
import os
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from modules.logger import setup_logger
from modules.routers import question
from modules.routers import user
from modules.routers import spot
# Load environment variables from .env file
load_dotenv()
# Setup logger
logger = logging.getLogger(__name__)
setup_logger(logger)
# Initialize the OpenAI ChatGPT model
openai.api_key = os.getenv("OPENAI_API_KEY")
model_name = "gpt-3.5-turbo"
# Create the FastAPI application
app = FastAPI()
# Enable CORS (Cross-Origin Resource Sharing)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allow all origins
allow_credentials=True,
allow_methods=["*"], # Allow all methods
allow_headers=["*"], # Allow all headers
)
# Include the routers
app.include_router(question.router)
app.include_router(user.router)
app.include_router(spot.router)
class ChatInput(BaseModel):
message: str
class ChatOutput(BaseModel):
message: str
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.post("/chat/test/spot")
async def chat(chat_input: ChatInput):
user_message = "제주도 안에 지역을 추천 받고 싶어, " + chat_input.message + "라는 지역은 어때? 한글로 답해줘"
print(f"Received a chat message: {user_message}")
# Send user message to ChatGPT and get the model's response
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": user_message,
}
],
)
print(completion)
completion_json = json.loads(str(completion))
content_msg = completion_json["choices"][0]["message"]["content"]
print(content_msg)
return ChatOutput(message=content_msg)
@app.post("/chat/test/mbti")
async def chat(chat_input: ChatInput):
user_message = (
"나의 성향을 알고 싶어, 나는 " + chat_input.message + "인데, MBTI 16가지 중에 뭘까? 한글로 답해줘"
)
print(f"Received a chat message: {user_message}")
# Send user message to ChatGPT and get the model's response
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": user_message,
}
],
)
print(completion)
completion_json = json.loads(str(completion))
content_msg = completion_json["choices"][0]["message"]["content"]
print(content_msg)
return ChatOutput(message=content_msg)