
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
import gradio as gr
import openai
openai.api_key ="sk-proj-ly4ROeTOuCyhWZYYYUkpT3BlbkFJQGAL7s0YNg51CLIBNIti"
def predict(message, history):
#OpenAl Chatgpt API에 전달할 메세지 형식
history_openai_format = []
#기존의 대화 목록을 OpenAI Chatgpt가 이해할 수 있도록 변환
for human, assistant in history:
history_openai_format.append({"role":"user", "content": human}) # 사용자의 메세질ㄹ 추가합니다.
history_openai_format.append({"role":"assistant", "content": assistant}) # chatgpt의 메세지를 추가합니다.
# 가장 최근에 입력된 사용자의 메세지를 추가합니다.
history_openai_format.append({"role":"user", "content": message})
# OPENAI ChatGPT API 호출
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # 어떤 모델(인공지능)을 쓸지 결정
messages=history_openai_format, #보낼 메세지(대화 기록 전송)
temperature=0.8, # 모델의 응답을 얼마나 다양하게 할건지 결정(0~1 사이의 값) -> 0이면 일괄된 답변, 1이면 창의적인 답변
stream=True # 응답을 스트리빙 형태로받을 것인지 여부
)
print(response)
partial_message = ""
for chunk in response:
if len(chunk['choices'][0]['delte']) != 0:
partial_message += chunk['choices'][0]['delta']['content']
yield partial_message
gr.ChatInterface(
fn=predict,
textbox=gr.Textbox(placeholder="말 걸어 주실래요?", container=False,scale=7)
).queue().launch(share=True, debug=True)

