Skip to content

AiDeepSeekChatDemo 项目分析

项目信息

文件结构

AiDeepSeekChatDemo/
├── Extractor_Icon.ico
├── Project1.py
├── Project1.xml
├── Unit1.py
├── Unit1.pydfm
├── Unit1.sct
├── Unit1.sfm

主程序文件: Project1.py

from glcl import *
from Unit1 import *

def main():
    icon_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Extractor_Icon.ico")
    if not os.path.exists(icon_path):
        print('Extractor_Icon.ico is missing. Do not delete or rename this file.')
        return
    Application.Initialize()
    Application.Title = 'Project1'
    Application.MainFormOnTaskbar = True
    Application.Icon.LoadFromFile(icon_path)
    MainForm = Form1(Application)
    MainForm.Show()
    FreeConsole()
    Application.Run()
    MainForm.Free()

if __name__ == '__main__':
    main()

单元文件: Unit1.py

# Powered By Python Studio, The best Python GUI IDE to download from glsite.com.
import os
from glcl import *

class Form1(Form):

    def __init__(self, owner):
        self.Button2 = Button(self)
        self.Button1 = Button(self)
        self.Memo2 = Memo(self)
        self.Memo1 = Memo(self)
        self.AiDeepSeekChat1 = AiDeepSeekChat(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.AiDeepSeekChat1.OnReceiveDataEnd = self.AiDeepSeekChat1ReceiveDataEnd
        self.AiDeepSeekChat1.OnReceiveData = self.AiDeepSeekChat1ReceiveData
        self.Button2.OnClick = self.Button2Click
        self.Button1.OnClick = self.Button1Click
        # 也可以直接在对象检查器设置属性
        self.AiDeepSeekChat1.ApiKey = '此处替换为你在DeepSeek API开放平台创建的API Key'

    # 第一种调用方式
    def Button1Click(self, Sender):
        # self.AiDeepSeekChat1.Asynchronous = True  # 也可以指定为异步的
        self.Memo1.Text = self.AiDeepSeekChat1.AddMessageAndRun(self.Memo2.Text, 'user', [])

    # 第二种调用方式
    def Button2Click(self, Sender):
        self.AiDeepSeekChat1.Asynchronous = True    # 必须指定异步输出,也可以直接设置对象检查器中的属性
        msg = self.AiDeepSeekChat1.AddMessage(self.Memo2.Text, 'user')
        self.AiDeepSeekChat1.Run(msg, None)

    # 第二种调用方式,设置事件
    def AiDeepSeekChat1ReceiveData(self, Sender, aMsg, aResponse, aRole, aText):
        try:
            self.Memo1.Lines.BeginUpdate()
            self.Memo1.Text += aText    # Memo文本内容每次加上接收的文本
            self.Memo1.Perform(EM_LINESCROLL, 0, self.Memo1.Lines.Count-1)  # 让Memo文本滚动到最底部一行
        finally:
            self.Memo1.Lines.EndUpdate()
            pass

    # 第二种调用方式,设置事件
    def AiDeepSeekChat1ReceiveDataEnd(self, Sender, aMsg, aResponse, aRole, aText):
        self.Memo1.Lines.Add('')    # 接收数据结束后换行

设计文件: Unit1.sct

def Button1Click(Sender): 
def Button2Click(Sender): 
def AiDeepSeekChat1ReceiveData(Sender, aMsg, aResponse, aRole, aText): 
def AiDeepSeekChat1ReceiveDataEnd(Sender, aMsg, aResponse, aRole, aText): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'TAiDeepSeekChatDemo'
  ClientHeight = 418
  ClientWidth = 481
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object Memo1: TMemo
    Left = 8
    Top = 8
    Width = 465
    Height = 233
    ScrollBars = ssBoth
    TabOrder = 0
  end
  object Memo2: TMemo
    Left = 8
    Top = 248
    Width = 465
    Height = 129
    ScrollBars = ssBoth
    TabOrder = 1
  end
  object Button1: TButton
    Left = 168
    Top = 384
    Width = 150
    Height = 25
    Caption = #31532#19968#31181#35843#29992#26041#24335
    TabOrder = 2
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 323
    Top = 384
    Width = 150
    Height = 25
    Caption = #31532#20108#31181#35843#29992#26041#24335
    TabOrder = 3
    OnClick = Button2Click
  end
  object AiDeepSeekChat1: TAiDeepSeekChat
    Model = 'deepseek-chat'
    Logprobs = False
    Max_tokens = 3000
    N = 1
    Response_format = tiaChatRfText
    Seed = 0
    Asynchronous = True
    Temperature = 1.00000000000000000
    Top_p = 1.00000000000000000
    Tool_choice = 'auto'
    Tool_Active = False
    User = 'user'
    InitialInstructions.Strings = (
      'Eres un asistente muy '#40407'il y servicial')
    Prompt_tokens = 0
    Completion_tokens = 0
    Total_tokens = 0
    OnReceiveData = AiDeepSeekChat1ReceiveData
    OnReceiveDataEnd = AiDeepSeekChat1ReceiveDataEnd
    Url = 'https://api.deepseek.com/v1/'
    ResponseTimeOut = 60000
    Stream_Usage = False
    NativeInputFiles = []
    NativeOutputFiles = []
    ChatMediaSupports = []
    Left = 445
    Top = 8
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

AI聊天功能演示

使用的组件

  • Form1 (TForm)
  • Memo1 (TMemo)
  • Memo2 (TMemo)
  • Button1 (TButton)
  • Button2 (TButton)
  • AiDeepSeekChat1 (TAiDeepSeekChat)

技术特点

  • 包含6个GUI组件
  • 定义2个事件处理

窗体属性

  • caption: #31532#20108#31181#35843#29992#26041#24335
  • height: 418
  • width: 481

代码分析

导入的模块: - import os - from glcl import *

定义的类: - Form1

定义的方法: - init - Button1Click - Button2Click - AiDeepSeekChat1ReceiveData - AiDeepSeekChat1ReceiveDataEnd

事件绑定: - self.Button2.OnClick = self.Button2Click - self.Button1.OnClick = self.Button1Click