Use Xunfei Xinghuo Cognitive Big Model to Build a Chatbot with Interface

In this blog, we will build a simple chatbot interface using the latest Spark Cognitive Mockup and Tkinter. Through this interface, users can interact with the chatbot and view the bot's answers.
C:\pythoncode\blog\static_16919367030710594_SparkApi_Python\testui.py
insert image description here

Preparation

Before we start, we need to prepare some work:

  • Install Python and Tkinter: Make sure you have Python installed on your computer and that the Tkinter module is working.

  • Obtain SparkApi key information: Obtain your SparkApi key information in the console, including APPID, APISecret, and APIKey. These information will be used to establish a connection with SparkApi and get answers.

  • Import the required modules: We'll use Tkinter to build the interface and a custom SparkApi module to communicate with the chatbot. tkinterMake sure the and modules are imported SparkApi.

create interface

First, we need to create a Tkinter window and some components to build the chatbot interface. Here is the code to create the interface:

import tkinter as tk
import SparkApi
from tkinter import messagebox

# 创建窗口
root = tk.Tk()
root.title("Chatbot")

# 创建文本框
textEdit = tk.Text(root)
textEdit.pack()

# 创建发送按钮
sendButton = tk.Button(root, text="发送", command=sendMessage)
sendButton.pack()

# 运行主循环
root.mainloop()

In the above code, we create a rootTkinter window named and set the title of the window as "Chatbot". Then, we created a text box textEditand a send button sendButton. Finally, use to root.mainloop()run Tkinter's main loop, keeping the window displayed.

Handling user input and getting answers

Next, we need to write functions to process user input and get responses from the chatbot. Here is the code for the handler function:

def sendMessage():
    # 获取用户输入的文本
    inputText = textEdit.get("1.0", tk.END).strip()

    # 将用户输入的文本发送给 SparkApi,并获取回答
    answer = SparkApi.getAnswer(inputText)

    # 将回答显示在文本框中
    textEdit.insert(tk.END, answer)

In the above code, we defined a sendMessage()function named as the callback function of the send button. Inside the function, we first textEdit.get("1.0", tk.END)obtain the text content entered by the user in the text box through and remove the leading and trailing blank characters. Then, we send the user's input to SparkApi and get the chatbot's answer. Finally, we use textEdit.insert(tk.END, answer)to display the answer in a text box.

run the program

After completing the writing of the interface and processing functions, we can run the program. Make sure you have configured the key information of SparkApi correctly, and the required dependencies have been installed on your computer. After running the program, you will see a simple chatbot interface, you can enter a question in the text box, and click the send button, the chatbot will give an answer and display it in the text box.

all codes

import tkinter as tk
import SparkApi
from tkinter import messagebox

def showMessage(message, title):
    messagebox.showinfo(title, message)

#以下密钥信息从控制台获取
appid = ""     #填写控制台中获取的 APPID 信息
api_secret = ""   #填写控制台中获取的 APISecret 信息
api_key =""    #填写控制台中获取的 APIKey 信息

#用于配置大模型版本,默认“general/generalv2”
domain = "general"   # v1.5版本

#云端环境的服务地址
Spark_url = "ws://spark-api.xf-yun.com/v1.1/chat"  # v1.5环境的地址
text =[]

# length = 0

def getText(role,content):
    jsoncon = {
    
    }
    jsoncon["role"] = role
    jsoncon["content"] = content
    text.append(jsoncon)
    return text

def getlength(text):
    length = 0
    for content in text:
        temp = content["content"]
        leng = len(temp)
        length += leng
    return length

def checklen(text):
    while (getlength(text) > 8000):
        del text[0]
    return text

def sendMessage():
    Input = textEdit.get("1.0", tk.END).strip()
    print(Input)
    question = checklen(getText("user",Input))
    print(question)
    SparkApi.answer =""
    print("星火:",end = "")
    SparkApi.main(appid,api_key,api_secret,Spark_url,domain,question)
    answer = getText("assistant",SparkApi.answer)
    answer_str = " ".join(answer)
    showMessage("Answer: " + answer_str, title="Answer")
    text.clear()
root = tk.Tk()
root.title("Chatbot")
textEdit = tk.Text(root)
textEdit.pack()
sendButton = tk.Button(root, text="发送", command=sendMessage)
sendButton.pack()
root.mainloop()

Summarize

In this blog, we built a simple chatbot interface using Python and Tkinter. Through this interface, users can interact with the chatbot and view the bot's answers. Through this simple interface, we can integrate chatbots into various applications to provide intelligent dialogue functions. I hope this blog is helpful to you, thank you for reading!

Reference example: https://www.xfyun.cn/doc/spark/Web.html#_2-%E8%B0%83%E7%94%A8%E7%A4%BA%E4%BE%8B

Guess you like

Origin blog.csdn.net/winniezhang/article/details/132375902