#!/usr/bin/env python # coding: utf-8 # ## ChatGPT来了,更快的速度更低的价格 # In[ ]: # In[ ]: import openai openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Who won the world series in 2020?"}, {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."}, {"role": "user", "content": "Where was it played?"} ] ) import openai import os OPENAI_API_KEY=os.environ.get("OPENAI_API_KEY") openai.api_key = OPENAI_API_KEY # 封装了一个 Conversation 类 class Conversation: # prompt 作为system 的 content,代表我们对这个聊天机器人的指令, # num_of_round 代表每次向ChatGPT 发起请求的时候,保留过去几轮会话。 def __init__(self, prompt, num_of_round): self.prompt = prompt self.num_of_round = num_of_round self.messages = [] self.messages.append({"role": "system", "content": self.prompt}) #输入是一个 string 类型的 question,返回结果也是 string 类型的一条 message。 # 每次调用 ask 函数,都会向 ChatGPT 发起一个请求 # 在这个请求里,我们都会把最新的问题拼接到整个对话数组的最后,而在得到 ChatGPT 的回答之后也会把回答拼接上去。 def ask(self, question): try: self.messages.append( {"role": "user", "content": question}) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=self.messages, temperature=0.5, max_tokens=2048, top_p=1, ) except Exception as e: print(e) return e message = response["choices"][0]["message"]["content"] self.messages.append({"role": "assistant", "content": message}) # 回答完之后,发现会话的轮数超过我们设置的 num_of_round,我们就去掉最前面的一轮会话 if len(self.messages) > self.num_of_round*2 + 1: del self.messages[1:3] return message import gradio as gr prompt = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求: 1. 你的回答必须是中文 2. 回答限制在100个字以内""" # 定义好了 system 这个系统角色的提示语,创建了一个 Conversation 对象。 conv = Conversation(prompt, 5) # 通过 history 维护了整个会话的历史记录 def predict(input, history=[]): history.append(input) response = conv.ask(input) history.append(response) # 通过 responses,将用户和 AI 的对话分组 responses = [(u,b) for u,b in zip(history[::2], history[1::2])] return responses, history # 最后,我们通过一段 with 代码,创建了对应的聊天界面。Gradio 提供了一个现成的Chatbot 组件,我们只需要调用它,然后提供一个文本输入框就好了。 with gr.Blocks(css="#chatbot{height:350px} .overflow-y-auto{height:500px}") as demo: chatbot = gr.Chatbot(elem_id="chatbot") state = gr.State([]) with gr.Row(): txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter") txt.submit(predict, [txt, state], [chatbot, state]) demo.launch() # In[ ]: