首页 > 其他分享 >极简代码使用gradio openai 搭建chatbot

极简代码使用gradio openai 搭建chatbot

时间:2024-08-18 19:51:45浏览次数:11  
标签:极简 gr format chatbot content openai message history

主要用来方便测试接口。
gr.ChatInterface() 是比 gr.Chatbot() 更高一级的封装
如果只是需要一个纯文字聊天的窗口(见下图),完全满足需求。
如果需要更多定制化的功能,比如定义prompt,显示图片等,那么就要使用gr.Chatbot() 开发。
修改为自己的接口,只需要修改predict_stream 或者predict函数,输入参数不变,输出为返回对话结果,str格式。
ps: 花费了几个小时研究gr.Chatbot(),其实gr.ChatInterface() 一行代码就够了。
更多需求请参考:creating-a-chatbot-fast

import os
import gradio as gr
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),  # 如果您没有配置环境变量,请在此处用您的API Key进行替换
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",  # 填写DashScope SDK的base_url
)

MODEL = "qwen-max"

#输入参数必须是 “用户输入”和“历史记录”,输出为字符串,变量名随便。
def predict_stream(message, history):
    history_openai_format = []
    for human, assistant in history:
        history_openai_format.append({"role": "user", "content": human})
        history_openai_format.append({"role": "assistant", "content": assistant})
    history_openai_format.append({"role": "user", "content": message})

    response = client.chat.completions.create(
        model=MODEL, messages=history_openai_format, temperature=1.0, stream=True
    )

    partial_message = ""
    for chunk in response:
        if chunk.choices[0].delta.content is not None:
            partial_message = partial_message + chunk.choices[0].delta.content
            yield partial_message


def predict(message, history):
    history_openai_format = []
    for human, assistant in history:
        history_openai_format.append({"role": "user", "content": human})
        history_openai_format.append({"role": "assistant", "content": assistant})
    history_openai_format.append({"role": "user", "content": message})

    response = client.chat.completions.create(
        model=MODEL, messages=history_openai_format, temperature=1.0, stream=False
    )
    return response.choices[0].message.content


gr.ChatInterface(predict).launch(server_name="127.0.0.1", server_port=8000, inbrowser=True)

在这里插入图片描述

标签:极简,gr,format,chatbot,content,openai,message,history
From: https://blog.csdn.net/zhilaizhiwang/article/details/141269973

相关文章

  • P1045 [NOIP2003 普及组] 麦森数极简解法解读
    源代码如下(这个精妙绝伦的算法不是我发现的,而是取自原题解中的某个大佬,在经过一顿学习正常题解后看到,顿觉豁然开朗,原贴:https://www.luogu.com.cn/article/c3u874kg)includeincludeincludeusingnamespacestd;longlonga[501]={1};intmain(){intp;cin>>p;cout<<(......
  • Langchain pandas agent - Azure OpenAI account
    Langchainpandasagent结合AzureOpenAI账户使用时,主要涉及到通过AzureOpenAI提供的自然语言处理能力,来操作pandasDataFrame或进行相关的数据处理任务。以下是关于这一结合使用的详细解析:一、Langchainpandasagent概述在LangChain中,Agent是一个核心概念,它代表了......
  • Linux打包命令tar极简示例_2
    只解压tar包中的某个文件这是tar包:只解压a.txt:上边的例子不大理想,再来一个tar包里带目录的:再弄个gzip压缩过的吧:......
  • C++趣味实验之:设计一个模拟公司运营的程序(极简版)
    根据剩余价值理论,设计一个模拟公司运营的程序原理非常简单: (此公式为企业扩大再生产的基本规律)同理,我们可以利用C++来实现这个操作,这就需要使用递归函数doublen,c,sum1,d1,z1;cout<<"输入启动资金(万元):"<<endl;cin>>n;intb;cout<<"输入市场劳动力数目:"<<endl;ci......
  • 【转载】为什么OpenAI下一步是Agent? 关于Agent你需要知道的一切
    单Agent不就是生物学中的细胞吗?多Agent不就是一个物种部落吗?单Agent不就是生物学中的细胞吗?多Agent不就是一个物种部落吗?大家好。我是甘润泽,毕业于硕士新加坡国立大学(NUS),深度学习方向,现在是AIAgent开发者、全栈工程师。很高兴在AI新智能的俱乐部内给大家做这次分享。我......
  • 简简单单,扫雷小游戏(极简版)
    目录前言一、扫雷要求二、准备工作1.建立文件2.游戏思路三、扫雷游戏代码步骤1:建立主函数步骤2:创建数组并初始化步骤3:打印数组步骤4:布置雷步骤5:排查雷步骤6:补全头文件四、完整代码 后言:前言用VS2022写的扫雷小游戏,超详细,一步一步帮你理透.一、......
  • 奥特曼花园私照“惊”到AI圈创始人,引出OpenAI代号“草莓”神秘项目进展大讨论,匿名基础
    今天,SamAltman在X上晒了一张自家花园的照片,结果却把AI界的创始人们“惊”到了。图片公司高层突发的巨大变动,似乎并没有太多影响到这位当家人,奥特曼在海外媒体秀出了一张花园花盆中生长的草莓的宜人景色,并用他典型的全小写文字风格配文“我喜欢花园里的夏天”。看起来,既在......
  • OpenAI API: How do I handle errors in Python?
    题意:在使用OpenAIAPI进行Python开发时,怎样处理错误?问题背景:Itriedusingthebelowcode,buttheOpenAIAPIdoesn'thavethe AuthenticationError methodinthelibrary.HowcanIeffectivelyhandlesucherror.我尝试使用下面的代码,但是OpenAIAPI的库中并没......
  • AttributeError: module ‘openai’ has no attribute ‘error’
    题意:访问Python中 openai 模块的一个不存在的属性 error问题背景:I'mrunningaPythonsummarizerinAzureMLthatqueriesmygpt4deploymentforinformation.Everythingwasworkingfineuntiltwodaysago,whenIdecidedtoswitchtheoutputdirectory......
  • openai 的各个模型比较(关于英语句子解析)
    最近在使用chatgpt帮助学习英语,主要是进行语法分析和难点解释。为了找到最适合的模型,我比较了多个模型的回答。语法分析问题这是我在实际中理解有困难的句子,尽管比较简短,但从内容上理解,它涉及了倒装。各个模型回答gpt-3.-5-turbo-1106是经过微调的3.5-turbogpt-4o-m......