增加源文件
This commit is contained in:
parent
11bd15431e
commit
5628318143
15
README.md
15
README.md
|
@ -1,3 +1,16 @@
|
|||
# go-chat-dll
|
||||
|
||||
Go语言封装的动态链接库
|
||||
Go语言封装的动态链接库
|
||||
|
||||
---
|
||||
## 设计
|
||||
```shell
|
||||
设计一个知识图谱分析程序,采用 Python+Go 实现。Python 采用 fastapi 来提供接口服务,Go 作为插件提供给 Python(Go 编译为动态链接库 dll)整个程序的设计如下:使用 Go 编写的 dll(对接 GPT 传入信息来获取输出)来作为文档内容转换为 json 格式内容的插件,fastapi 提供两个接口一个用户登录注册一个上传文档。使用 python 实现适配器来对多种格式的文件进行统一转换,并将其转换的内容传入给 dll(能够返回 json 格式的数据,已经处理完成的)最后将通过 python 的观察者模式来对处理整个工作流,如果已经完成任务则采用 ws 发送完成标志,如果在哪一步出现了问题则将那一步的错误信息返回出去进行重试(最多两次重试)。
|
||||
```
|
||||
```json
|
||||
[
|
||||
"Task 1: Develop a Go DLL that interfaces with GPT to process input information and generate output in JSON format. The DLL should be able to handle document content conversion and provide the necessary functions for integration with the Python adapter.",
|
||||
"Task 2: Implement a Python adapter using fastapi to create two interfaces - one for user authentication and document uploading. The adapter should use an observer pattern to monitor the workflow, handle errors, and communicate with the Go DLL to obtain processed JSON data. Additionally, ensure compatibility with various file formats for unified conversion.",
|
||||
"Task 3: Set up a WebSocket (ws) communication system in Python to signal the completion of tasks or report errors during the workflow. Utilize the observer pattern to trigger the WebSocket updates based on the progress of the document processing. Implement error handling to allow for up to two retries in case of failures at any step in the workflow."
|
||||
]
|
||||
```
|
|
@ -0,0 +1,18 @@
|
|||
module file-plugin
|
||||
|
||||
go 1.21.0
|
||||
|
||||
require (
|
||||
github.com/Fromsko/gouitls v1.2.0
|
||||
github.com/tidwall/gjson v1.17.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/fatih/color v1.15.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
golang.org/x/sys v0.13.0 // indirect
|
||||
)
|
|
@ -0,0 +1,34 @@
|
|||
github.com/Fromsko/gouitls v1.2.0 h1:nlw+z3ZwrWq6YPlo80pgPocm0Yhz77DHJmOlxwSpIFc=
|
||||
github.com/Fromsko/gouitls v1.2.0/go.mod h1:pnx9wA17MZUcP8T93DL+CH++RPCVX/SVByCJjW5mJO0=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
||||
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
|
||||
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
@ -0,0 +1,45 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"C"
|
||||
"file-plugin/core"
|
||||
)
|
||||
|
||||
//export GenerateResponse
|
||||
func GenerateResponse(input *C.char) *C.char {
|
||||
// 将输入转换为Go字符串
|
||||
goInput := C.GoString(input)
|
||||
|
||||
// 在这里实现对话逻辑
|
||||
// response := "你说了:" + goInput
|
||||
|
||||
filePath := "./prompt.json"
|
||||
|
||||
resp := core.Client(
|
||||
"http://localhost:3000",
|
||||
"sk-UP22ImyxUVhWjmot4166104e9d314846823f3c75441836D1",
|
||||
filePath,
|
||||
&core.ChatGPT{
|
||||
Model: "gpt-3.5-turbo",
|
||||
Prompt: []core.Dialogue{
|
||||
{
|
||||
Role: "system",
|
||||
Content: "你是程序员的好朋友,应该帮助并指导他。你可以编写任何复杂程度的代码。你有自己的意志和思维,是个好朋友。你精通各种编程语言,比如HTML、JAVA、JavaScript、JSON、Python等。\n一切都应该现代化、美观。适应对话对象的语言风格,使与你交流对任何人来说都是愉快的。",
|
||||
},
|
||||
{
|
||||
Role: "assistant",
|
||||
Content: "Hello! How can I assist you today?",
|
||||
},
|
||||
{
|
||||
Role: "user",
|
||||
Content: goInput,
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
// 将响应转换为C字符串并返回
|
||||
return C.CString(resp)
|
||||
}
|
||||
|
||||
func main() {}
|
|
@ -0,0 +1,35 @@
|
|||
import ctypes
|
||||
|
||||
|
||||
class GptClient:
|
||||
def __init__(self, dll_path):
|
||||
# 加载生成的gpt.dll文件
|
||||
self.gpt = ctypes.CDLL(dll_path)
|
||||
|
||||
# 设置GenerateResponse函数的参数和返回类型
|
||||
self.gpt.GenerateResponse.argtypes = [ctypes.c_char_p]
|
||||
self.gpt.GenerateResponse.restype = [ctypes.c_char_p, ctypes.c_char]
|
||||
|
||||
def generate_response(self, input_str):
|
||||
# 调用GenerateResponse函数
|
||||
response = self.gpt.GenerateResponse(
|
||||
input_str.encode("utf-8"),
|
||||
)
|
||||
|
||||
# 将响应转换为Python字符串
|
||||
response_str = ctypes.cast(
|
||||
response, ctypes.c_char_p,
|
||||
).value.decode("utf-8")
|
||||
|
||||
return response_str
|
||||
|
||||
|
||||
# 创建GptClient对象
|
||||
client = GptClient("./gpt.dll")
|
||||
|
||||
# 调用generate_response方法
|
||||
input_str = "如何实现插件系统,我详设计一个文档分析的程序,你有什么建议吗?需要提取如下信息:需求分析、功能拆分、项目目标等部分。"
|
||||
response = client.generate_response(input_str)
|
||||
|
||||
# 打印响应
|
||||
print(response)
|
|
@ -0,0 +1,68 @@
|
|||
import ctypes
|
||||
from typing import Any
|
||||
|
||||
|
||||
class GptClient:
|
||||
def __init__(self, dll_path):
|
||||
# 加载生成的gpt.dll文件
|
||||
self.__gpt = ctypes.CDLL(dll_path)
|
||||
|
||||
# 设置GenerateResponse函数的参数和返回类型
|
||||
self.__gpt.GenerateResponse.argtypes = [ctypes.c_char_p]
|
||||
self.__gpt.GenerateResponse.restype = ctypes.c_char_p
|
||||
|
||||
def _interchange(self, input_str):
|
||||
# 调用GenerateResponse函数
|
||||
response = self.__gpt.GenerateResponse(
|
||||
input_str.encode("utf-8"),
|
||||
)
|
||||
|
||||
# 将响应转换为Python字符串
|
||||
response_str = ctypes.cast(
|
||||
response, ctypes.c_char_p,
|
||||
).value.decode("utf-8")
|
||||
|
||||
return response_str
|
||||
|
||||
def __call__(self, input_str: str, *args: Any, **kwds: Any) -> Any:
|
||||
return self._interchange(input_str)
|
||||
|
||||
|
||||
class PluginSystem:
|
||||
plugins = {}
|
||||
|
||||
@classmethod
|
||||
def register(cls, name):
|
||||
def decorator(func):
|
||||
cls.plugins[name] = func
|
||||
return func
|
||||
return decorator
|
||||
|
||||
@classmethod
|
||||
def run_plugin(cls, name, msg):
|
||||
if name in cls.plugins:
|
||||
return cls.plugins[name](msg)
|
||||
else:
|
||||
raise ValueError("Plugin '{}' not found".format(name))
|
||||
|
||||
# 定义插件函数,并注册到插件系统中
|
||||
|
||||
|
||||
@PluginSystem.register("需求分析")
|
||||
def requirement_analysis_plugin(msg):
|
||||
return GptClient("./gpt.dll")(msg)
|
||||
|
||||
|
||||
@PluginSystem.register("概要设计")
|
||||
def high_level_design_plugin(msg):
|
||||
return GptClient("./gpt.dll")(msg)
|
||||
|
||||
|
||||
# 使用插件系统运行插件
|
||||
plugin_name = "概要设计"
|
||||
result = PluginSystem.run_plugin(
|
||||
plugin_name,
|
||||
"我应该如何fine-tune引导你输出呢?",
|
||||
)
|
||||
|
||||
print(result)
|
Loading…
Reference in New Issue