langchain-go调用deepseek

1.查看官网

在这里插入图片描述

发现只有ollama,openai,Mistral于是查看代码

2.代码查看

先从llm, err := openai.New(url, model, token)开始

在这里插入图片描述

发现New方法可以传option参数,再看一下option参数
const (
	tokenEnvVarName        = "OPENAI_API_KEY"      //nolint:gosec
	modelEnvVarName        = "OPENAI_MODEL"        //nolint:gosec
	baseURLEnvVarName      = "OPENAI_BASE_URL"     //nolint:gosec
	baseAPIBaseEnvVarName  = "OPENAI_API_BASE"     //nolint:gosec
	organizationEnvVarName = "OPENAI_ORGANIZATION" //nolint:gosec
)

type APIType openaiclient.APIType

const (
	APITypeOpenAI  APIType = APIType(openaiclient.APITypeOpenAI)
	APITypeAzure           = APIType(openaiclient.APITypeAzure)
	APITypeAzureAD         = APIType(openaiclient.APITypeAzureAD)
)

const (
	DefaultAPIVersion = "2023-05-15"
)

type options struct {
	token        string
	model        string
	baseURL      string
	organization string
	apiType      APIType
	httpClient   openaiclient.Doer

	responseFormat *ResponseFormat

	// required when APIType is APITypeAzure or APITypeAzureAD
	apiVersion     string
	embeddingModel string

	callbackHandler callbacks.Handler
}

// Option is a functional option for the OpenAI client.
type Option func(*options)

// ResponseFormat is the response format for the OpenAI client.
type ResponseFormat = openaiclient.ResponseFormat

// ResponseFormatJSON is the JSON response format.
var ResponseFormatJSON = &ResponseFormat{Type: "json_object"} //nolint:gochecknoglobals

// WithToken passes the OpenAI API token to the client. If not set, the token
// is read from the OPENAI_API_KEY environment variable.
func WithToken(token string) Option {
	return func(opts *options) {
		opts.token = token
	}
}

// WithModel passes the OpenAI model to the client. If not set, the model
// is read from the OPENAI_MODEL environment variable.
// Required when ApiType is Azure.
func WithModel(model string) Option {
	return func(opts *options) {
		opts.model = model
	}
}

// WithEmbeddingModel passes the OpenAI model to the client. Required when ApiType is Azure.
func WithEmbeddingModel(embeddingModel string) Option {
	return func(opts *options) {
		opts.embeddingModel = embeddingModel
	}
}

// WithBaseURL passes the OpenAI base url to the client. If not set, the base url
// is read from the OPENAI_BASE_URL environment variable. If still not set in ENV
// VAR OPENAI_BASE_URL, then the default value is https://api.openai.com/v1 is used.
func WithBaseURL(baseURL string) Option {
	return func(opts *options) {
		opts.baseURL = baseURL
	}
}

// WithOrganization passes the OpenAI organization to the client. If not set, the
// organization is read from the OPENAI_ORGANIZATION.
func WithOrganization(organization string) Option {
	return func(opts *options) {
		opts.organization = organization
	}
}

// WithAPIType passes the api type to the client. If not set, the default value
// is APITypeOpenAI.
func WithAPIType(apiType APIType) Option {
	return func(opts *options) {
		opts.apiType = apiType
	}
}

// WithAPIVersion passes the api version to the client. If not set, the default value
// is DefaultAPIVersion.
func WithAPIVersion(apiVersion string) Option {
	return func(opts *options) {
		opts.apiVersion = apiVersion
	}
}

// WithHTTPClient allows setting a custom HTTP client. If not set, the default value
// is http.DefaultClient.
func WithHTTPClient(client openaiclient.Doer) Option {
	return func(opts *options) {
		opts.httpClient = client
	}
}

// WithCallback allows setting a custom Callback Handler.
func WithCallback(callbackHandler callbacks.Handler) Option {
	return func(opts *options) {
		opts.callbackHandler = callbackHandler
	}
}

// WithResponseFormat allows setting a custom response format.
func WithResponseFormat(responseFormat *ResponseFormat) Option {
	return func(opts *options) {
		opts.responseFormat = responseFormat
	}
}
这里发现了各种配置的地方,以及获取环境变量,于是我们使用openai包下面的各种with方法来配置deepseek的地址
// 我这里使用的是腾讯的API
url := openai.WithBaseURL("https://api.lkeap.cloud.tencent.com/v1/chat/completions")
	model := openai.WithModel("deepseek-v3")
	token := openai.WithToken("API-KEY")
	// We can construct an LLMChain from a PromptTemplate and an LLM.
	llm, err := openai.New(url, model, token)
	if err != nil {
		return err
	}
	ctx := context.Background()
	completion, err := llm.Call(ctx, "The first man to walk on the moon",
		llms.WithTemperature(0.8),
		llms.WithStopWords([]string{"Armstrong"}))

	if err != nil {
		return err
	}
	fmt.Println(completion)
	return nil
### 如何使用 LangChain-Chatchat 调用 GraphRAG 进行查询 GraphRAG (Retrieval-Augmented Generation) 是一种结合了检索增强生成的方法,旨在通过利用外部知识库来提高自然语言处理任务的效果。当考虑如何使用 `langchain-chatchat` 来调用 GraphRAG 进行查询时,可以遵循以下方法论。 #### 构建环境并安装必要的依赖项 在开始之前,确保已经设置了适当的工作环境,并安装了所需的 Python 库: ```bash pip install -qU langchain langchain-community langchain-openai youtube-transcript-api pytube langchain-chroma ``` 这一步骤是为了准备开发环境中所需要的工具集[^2]。 #### 创建 GraphRAG 查询接口 对于具体实现来说,假设已经有了一个可用的 GraphRAG API 或者服务端点。接下来就是构建客户端逻辑以发送请求给这个API和服务端点。通常情况下,这样的过程涉及以下几个方面: - **定义PromptTemplate**: 设计合适的提示词模版,以便向 GraphRAG 提供清晰的任务描述以及上下文信息。 - **配置 ChatModel 和其他组件**: 使用类似于下面的方式初始化 LLM聊天代理所需的各种组成部分,包括但不限于设置好 PromptTemplate, ChatModel, Stop序列和 OutputParser 等要素[^4]。 ```python from langchain import PromptTemplate, LLMChatAgent prompt_template = "根据提供的文档片段{context}回答关于 {question} 的问题." stop_sequence = ["\n"] output_parser = ... agent = LLMChatAgent( prompt=PromptTemplate(template=prompt_template), llm_model="path_to_your_chatmodel", # 替换成实际路径或名称 stop_sequences=[stop_sequence], output_parser=output_parser, ) ``` 请注意,在这里 `"path_to_your_chatmodel"` 需要替换为指向所使用的特定聊天模型的实际位置或者标识符;而 `output_parser` 则应该被实例化为你想要用来解析返回数据的具体类对象。 #### 发送查询至 GraphRAG 并获取响应 一旦完成了上述准备工作之后,则可以通过如下方式发起针对 GraphRAG 数据源的查询操作: ```python messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "我想知道有关LangChain的知识"}, ] response = agent.run(messages) print(response['answer']) ``` 这段代码展示了怎样构造消息列表并通过运行代理来进行一次完整的交互流程,最终打印出由 GraphRAG 处理后的答案[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值