-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (41 loc) · 1.33 KB
/
main.py
File metadata and controls
49 lines (41 loc) · 1.33 KB
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
import chainlit as cl
import os
from dotenv import load_dotenv
import requests
# .env 파일을 로드합니다.
load_dotenv()
# 환경 변수에서 Gemini API 키를 가져옵니다.
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
# 최신 Gemini API 엔드포인트 및 모델명
GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"
@cl.on_message
async def main(message: cl.Message):
user_input = message.content
if not GEMINI_API_KEY:
response = "GEMINI_API_KEY가 설정되지 않았습니다."
await cl.Message(content=response).send()
return
headers = {
"Content-Type": "application/json"
}
params = {
"key": GEMINI_API_KEY
}
data = {
"contents": [
{
"parts": [
{"text": user_input}
]
}
]
}
try:
resp = requests.post(GEMINI_API_URL, headers=headers, params=params, json=data, timeout=30)
resp.raise_for_status()
result = resp.json()
llm_response = result["candidates"][0]["content"]["parts"][0]["text"]
response = f"Gemini 응답: {llm_response}"
except Exception as e:
response = f"Gemini API 호출 중 오류 발생: {e}"
await cl.Message(content=response).send()