golang调用deepseekr1

1.官方文档

在这里插入图片描述

2.请求看一下

因为deepseek官方API的deepssek-r1响应太慢,于是用了腾讯的API来测试
func main() {
	cfg := config.Config{
		BaseURL:    "https://api.lkeap.cloud.tencent.com",
		APIKey:     "API-KEY",
		HTTPClient: &http.Client{},
	}
	// 初始化deepseek
	d := deepseek.NewDeepSeek(cfg)
	// 封装请求体
	body := model.Request{
		Model:    "deepseek-r1",
		Messages: []model.Message{{Role: "system", Content: "You are a helpful assistant."}, {Role: "user", Content: "你是谁"}},
	}
	// 同步调用
	chat, err := d.Chat(context.Background(), body)
	if err != nil {
		panic(err)
	}
	fmt.Println(chat.Content)
	// 流式调用
	//stream, _ := d.Stream(context.Background(), body)
	//console.ConsoleContent(stream)
}

响应的格式

{"id":"ab6e60c5df865a38a","object":"chat.completion","created":1739782294,"model":"deepseek-r1","choices":[{"index":0,"message":{"role":"assistant","content":"\n\n您好!我是DeepSeek-R1,一个由深度求索公司开发的智能学,代码和逻辑推理等理工类问题。我会保持诚实,如果不知道答案会明确告知,并始终聚焦于提供最有帮助的信息。","reasoning_content":"\n嗯,用户问我是谁,我需要用中文回答。首先,我要明确自己的身份,是DeepSeek-R1,一个由深度求索公司开发,可能无法提供最新信息。同时,要避免使用复杂术语,保持口语化,让回答更自然。可能用户刚接触我,所以需要简洁明了,不需要太长的解释。还要注意语气友好,表现出愿意帮助的态度。有没有遗漏什么?比如是否需要提到公司背景或者隐私政策?不过ens":230,"total_tokens":246}}

发现跟deepseek-chat的响应只是多路一个reasoning_content,在响应上面加上就可以了

type Response struct {
	Content          string `json:"content"`
	ReasoningContent string `json:"reasoning_content"` // 思考回答
}

3.改造后的整个访问流程

1. 调用流式请求
   stream, _ := d.Stream(context.Background(), body)
2. 发起流式请求
   func (d *DeepSeek) Stream(ctx context.Context, request model.Request) (<-chan model.Response, error) {
	return http_parse.HandleStreaming(ctx, request, &d.Cfg)
}
3. 响应处理
	func HandleStreaming(ctx context.Context, req model.Request, c *config.Config) (<-chan model.Response, error) {
	ch := make(chan model.Response)
	go func() {
		defer close(ch)
		// 发起流式请求
		resp, err := DoRequest(ctx, req, true, c)
		if err != nil {
			ch <- model.Response{Content: "request error!"}
			return
		}
		defer resp.Body.Close()

		scanner := bufio.NewScanner(resp.Body)
		for scanner.Scan() {
			select {
			case <-ctx.Done():
				ch <- model.Response{Content: "ctx done!"}
				return
			default:
				// 解析事件流
				event := ParseEvent(scanner.Bytes())
				if event != nil {
					ch <- *event
				}
			}
		}
	}()

	return ch, nil
}

4. 打印结果
	func Content(ch <-chan model.Response) {
	for chunk := range ch {
		if chunk.ReasoningContent != "" {
			fmt.Printf(chunk.ReasoningContent)
			continue
		}
		// 打印回答
		fmt.Printf(chunk.Content)
	}
}

4.代码地址

https://gitee.com/li-zhuoxuan/go_ai

### 调用 Windows DLL 的方法 为了在 Go 中调用 Windows DLL 文件中的函数,可以利用 `syscall.Syscall` 或者更推荐的方式是使用标准库包 `golang.org/x/sys/windows` 提供的功能。下面展示了一个简单的例子说明如何加载一个DLL并调用其中的一个过程。 #### 加载 DLL 和 获取 函数指针 首先定义两个变量用于保存模块句柄和函数地址: ```go package main import ( "fmt" "log" "unsafe" "golang.org/x/sys/windows" ) var ( user32 = windows.NewLazySystemDLL("user32.dll") // 创建指向 user32.dll 的 LazyDLL 实例 messageBox = user32.NewProc("MessageBoxW") // 从该实例创建指向 MessageBoxA 导出函数的 Proc 对象 ) ``` 这段代码初始化了对 `user32.dll` 库及其导出函数 `MessageBoxW` 的访问[^1]。 #### 使用 Syscall 调用函数 接着可以通过这些对象来进行实际的过程调用: ```go func main() { ret, _, err := messageBox.Call( uintptr(0), // hWnd (handle of window) windows.StringToUTF16Ptr("Hello!"), // lpText (text in dialog box) windows.StringToUTF16Ptr("Title Bar"), // lpCaption (title bar text) uintptr(0x00000040)) // uType (style of dialog box) if ret == 0 { log.Fatalf("Call failed with error: %v", err) } fmt.Println("Message Box returned:", ret) } ``` 此部分展示了怎样通过 `Call()` 方法执行之前获得的函数指针,并传递必要的参数给它。这里选择了 `MessageBoxW` 来显示一条消息框作为演示目的。 对于其他类型的API调用,则需按照相应文档准备合适的参数列表;同时注意数据类型转换问题,比如字符串编码等细节处理。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值