基于langchain的开源大模型应用开发1

服务端grpc框架

server-grpc

etc yaml配置及

internal 内部代码包

config yaml配置解析代码包

logic 逻辑实现包

server 服务连接处理

svc 上下文配置信息

proto proto文件

go.mod model

go.sum

main.go 主函数入口

逻辑代码处理

目前该应用的逻辑只有机器人对话功能,在服务端是通过grpc服务进行数据传输,这里前提是在main函数中需要启动grpc服务,设置proto数据类型。在logic层初始化服务上下文信息,日治配置大模型配置等。

java 复制代码
func NewChatbotLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatbotLogic {

	llm, err := chain.New(chain.WithToken(svcCtx.Config.OpenaiClient.Token), chain.WithModel(svcCtx.Config.OpenaiClient.Model), chain.WithBaseURL(svcCtx.Config.OpenaiClient.BaseURL))
	if err != nil {
		logx.Errorf("large model import error: %v", err)
		return nil
	}
	return &ChatbotLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
		model:  llm,
	}
}

在对话逻辑中,设置历史信息的上限,设置对话者角色,从客户端获取用户对话信息。通过langchain框架传递给大模型返回信息,最后将得到最准确的信息返回给用户。

java 复制代码
// 对话逻辑
func (c *ChatbotLogic) Chat(req *pb.ChatRequest) (*pb.ChatResponse, error) {
	//chat role and part
	message := make([]llms.MessageContent, 10)
	var index int = 0
	if index == 9 {
		index = 0
	}
	//create role instance
	message[index].Role = "human"
	//create text instance
	text := llms.TextContent{Text: req.UserInput}
	message[index].Parts = []llms.ContentPart{text}
	//input session
	respose, err := c.model.GenerateContent(c.ctx, message)
	if err != nil {
		logx.Errorf("Session error: %v", err)
		return nil, err

	}
	jsonRespponse, err := json.Marshal(respose)
	if err != nil {
		logx.Errorf("struct parse error: %v", err)
		result := respose.Choices[0].Content
		return &pb.ChatResponse{
			BotResponse: result,
		}, err
	}
	jsonRes := string(jsonRespponse)
	return &pb.ChatResponse{
		BotResponse: jsonRes,
	}, nil
}

下一期敬请期待。

相关推荐
q***71853 小时前
【golang学习之旅】使用VScode安装配置Go开发环境
vscode·学习·golang
i***48613 小时前
【漏洞复现】CVE-2019-11043(PHP远程代码执行漏洞)信息安全论文_含漏洞复现完整过程_含Linux环境go语言编译环境安装
linux·golang·php
q***78378 小时前
【Golang】——Gin 框架中间件详解:从基础到实战
中间件·golang·gin
_小九11 小时前
【开源】耗时数月、我开发了一款功能全面【30W行代码】的AI图床
前端·后端·开源
serve the people11 小时前
HTML Document Loaders in LangChain
chrome·langchain·html
万事可爱^13 小时前
GitHub爆火开源项目——RustScan深度拆解
c语言·开发语言·rust·开源·github·rustscan
ApacheSeaTunnel13 小时前
(四)收官篇!从分层架构到数据湖仓架构系列:数据服务层和数据应用层及湖仓技术趋势小结
数据库·开源·数据湖·技术分享·白鲸开源
AI大模型14 小时前
5步构建企业级RAG应用:Dify与LangChain v1.0集成实战
langchain·llm·agent
Ryan ZX15 小时前
【Go语言基础】Go语言开发环境搭建
开发语言·后端·golang