AI接入微信公众号方法总结

 环境准备

2.AI的访问api-key,这个需要科学上网申请个账号,获取api-key并保存好。

3.一台云服务器。各大云商有提供,包年很便宜,部署下Golang的后台服务。

实现思路

总体方案介绍

客服回复消息接口

需要注意的是,需要使用golang的协程异步处理,因为用户发送信息后若5秒内没响应,会报服务异常的。异步示例如下:

var wxReceiveFunc = func(msg wxapi.WxReceiveCommonMsg) error {
	fmt.Println("weixin msg received")
	fmt.Printf("%#v
", msg)
	touser := msg.FromUserName
	content := msg.Content
	accessToken := wxapi.WxGetAccessToken()

	//异步请求chatAI,成功后调用客服接口回复
    go func(){
		resp:=chatapi.AskChatAI(content)
		if(resp !=""){
			wxapi.WxPostCustomTextMsg(accessToken,touser,resp)
		}else{
			wxapi.WxPostCustomTextMsg(accessToken,touser,"chatGPT服务异常")
		}
	}()
	
	return nil
}

text-davinci-003介绍

使用text-davinci-003的模型的ai的接口,详细的介绍参照地址:

https://beta.openai.com/docs/api-reference/completions/create#completions/create-model

关于text-davinci-003和官方AI的区别,参照下面文章,其实用起来差别不大。甚至text-davinci-003在写作创造方面更优秀。简单说AI是个产品,text-davinci-003更面向开发者开发产品使用。

curl https://api.openai.com/v1/completions 
-H "Content-Type: application/json" 
-H "Authorization: Bearer YOUR_API_KEY" 
-d {"model": "text-davinci-003", "prompt": "Say this is a test", "temperature": 0, "max_tokens": 7}
{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "

This is indeed a test",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 7,
    "total_tokens": 12
  }
}

官方貌似只提供了python的demo和接口实现,其实模拟http调用使用其他语言都可以实现。

main.go代码实现

至于里面的每日发送是啥?

大盘信息获取接口实现:

每日天气接口实现:

其他资源

https://beta.openai.com/docs/api-reference/completions/create#completions/create-model

经验分享 程序员 微信小程序 职场和发展