基于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
}

下一期敬请期待。

相关推荐
进击的程序猿~8 小时前
Go Zero源码阅读1
后端·golang
修己xj9 小时前
告别排版噩梦:一个开源SKILL,让我彻底告别公众号排版的“噩梦”
开源
飞猪~14 小时前
LangChain python 版本 第一集
开发语言·python·langchain
YIAN14 小时前
LangChain JS 工具调用实战:基于 DeepSeek-V4-Flash 实现本地文件读取 AI 代码助手(完整可运行源码)
javascript·langchain·前端框架
李燚16 小时前
Go 项目怎么组织:DDD 4 层 vs MVC vs 脚本式
开发语言·golang·mvc·ddd·agent框架·eino
齐 飞16 小时前
LangGraph快速入门-02状态
人工智能·langchain
椰椰椰耶17 小时前
【LangChain系列十二】RAG全流程下:从零搭建知识问答系统
langchain
Darling噜啦啦18 小时前
手写 mini-cursor 完整版:从 Promise 子进程到 ReAct 循环,30 轮迭代完成 TodoList 全流程
langchain·agent
GitCode官方18 小时前
开源鸿蒙跨平台直播| RN 三方库开源鸿蒙标准化适配实战分享
华为·开源·harmonyos·atomgit
梦梦代码精18 小时前
LikeShop 前后端架构升级对比总白皮书——从传统商城架构到高性能多端统一体系
docker·架构·开源