赞
踩
今天拿到了 Azure 授权的 chatGPT4 的接口,内心很激动,终于可以安心使用chatGPT4了。赶紧采用 gradio 组件封装了一个简单的对话窗口来体验一下 GPT4 的超能力,看似简单的功能在显示打字机效果时,还是花了一些时间思考。
代码如下:
1)先安装 python 的 gradio 组件
pip install gradio
2)运行如下代码,尽情享受超能力吧
- import time
- import gradio as gr
- import mdtex2html
-
- import os
- import openai
- openai.api_type = "azure"
- openai.api_base = "https://xxxx.openai.azure.com/"
- openai.api_version = "2023-05-15"
- openai.api_key = "API_KEY"
-
- # This will correspond to the custom name you chose for your deployment when you deployed a model.
- deployment_name = 'XXXXXXX'
-
-
- prompt = """
- 我想让你充当数学历史老师,提供有关数学概念的历史发展和不同数学家的贡献的信息。
- 你应该只提供信息而不是解决数学问题。使用以下格式回答:“{数学家/概念} - {他们的贡献/发展的简要总结}。
- 我的第一个问题是“毕达哥拉斯对数学的贡献是什么?”
- """
- prompt = ""
-
-
- def predict(chatbot, input, history, temperature, max_tokens, top_p, chat_turns=0):
- chat_turns = chat_turns +1
- chatbot.append((input, ""))
- history.append(input)
- messages = []
- messages.append({"role": "system", "content": prompt})
-
- try:
- # print("==========request======:", question)
- messages.append({"role": "user", "content": input})
- response = openai.ChatCompletion.create(
- engine=deployment_name,
- messages=messages,
- temperature=temperature,
- max_tokens=max_tokens,
- top_p=top_p,
- frequency_penalty=0,
- presence_penalty=0,
- stop=None,
- stream=True,
- )
- except Exception as e:
- print(e)
- return e
-
-
- content = {'role': '', 'content': ''}
- for event in response:
- if event['choices'][0]['finish_reason'] == 'stop':
- print(f'收到的完成数据: {content}')
- history.append(content["content"])
- break
- for delta_k, delta_v in event['choices'][0]['delta'].items():
- print(f'流响应数据: {delta_k} = {delta_v}')
- content[delta_k] += delta_v
- responses = [(u, b) for u, b in zip(history[::2], history[1::2])]
- # responses = [(input, content[delta_k])]
- responses.append((input, content[delta_k]))
- yield responses, history, chat_turns
-
-
- def reset_user_input():
- return gr.update(value='')
-
-
- def reset_state():
- return [], [],0
-
-
- with gr.Blocks() as demo:
- gr.HTML("""<h1 align="center">悦高软件-人工智能数字助手 GPT-4</h1>""")
- # radio = gr.Radio(value='gpt-3.5-turbo',
- # choices=['gpt-3.5-turbo', 'gpt-4'], label='models')
- chatbot = gr.Chatbot()
- with gr.Row():
- with gr.Column(scale=4):
- with gr.Column(scale=12):
- user_input = gr.Textbox(show_label=False, placeholder="请输入内容...", lines=12).style(
- container=False)
- with gr.Column(min_width=32, scale=1):
- submitBtn = gr.Button("提交", variant="primary")
- with gr.Column(scale=1):
- emptyBtn = gr.Button("重新对话")
- max_length = gr.Slider(
- 0, 4096, value=800, step=1.0, label="Maximum length", interactive=True)
- top_p = gr.Slider(0, 1, value=0.95, step=0.01,
- label="Top P", interactive=True)
- temperature = gr.Slider(
- 0, 1, value=0.7, step=0.01, label="Temperature", interactive=True)
- chat_turns = gr.Slider(1,20,value=0,step=1,label="对话轮数",info="记录历史对话轮数",interactive=False)
- state = gr.State([])
-
- submitBtn.click(predict, inputs=[chatbot, user_input, state, temperature, max_length, top_p, chat_turns], outputs=[
- chatbot, state, chat_turns], queue=True, show_progress=True)
- submitBtn.click(reset_user_input, [], [user_input])
- emptyBtn.click(reset_state, outputs=[
- chatbot, state, chat_turns], show_progress=True)
-
-
- demo.title = "Yogoo AI"
- demo.queue()
- demo.launch(share=False, inbrowser=True)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。