Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

文心一言 (测试版) 接入 #154

Open
zhayujie opened this issue Mar 18, 2023 · 18 comments
Open

文心一言 (测试版) 接入 #154

zhayujie opened this issue Mar 18, 2023 · 18 comments

Comments

@zhayujie
Copy link
Owner

zhayujie commented Mar 18, 2023

地址: https://yiyan.baidu.com/

配置:

"model": {
    "type" : "baidu",
    "baidu": {
          "acs_token": "YOUR ACS TOKEN",
          "cookie": "YOUR COOKIE"
     }
}

配置获取:
目前 Acs-Token 的有效期未知,待进一步优化:
image

@zhayujie
Copy link
Owner Author

zhayujie commented Mar 18, 2023

效果图,以wechat通道为例:

image

画图:
image

图片内容:
image

@zhayujie zhayujie pinned this issue Mar 18, 2023
@icejean
Copy link

icejean commented Mar 18, 2023

接入了发个测试报告参观一下。 :)

@Ayuyyae
Copy link

Ayuyyae commented Mar 19, 2023

发些截图我们参观一下

@RockChinQ
Copy link

实测acs-token就几分钟存活期,不进行对话一会儿就失效了;得研究一下怎么获取acs-token

@kse-ElEvEn
Copy link

目前有解决办法了吗

@stakeswky
Copy link

真奇怪,我复制cookie进去一直报格式错误:json.decoder.JSONDecodeError: Expecting ',' delimiter: line 6 column 1405 (char 1861)

@RockChinQ
Copy link

真奇怪,我复制cookie进去一直报格式错误:json.decoder.JSONDecodeError: Expecting ',' delimiter: line 6 column 1405 (char 1861)

去浏览器插件商店搜索“cookie editor”,使用这个插件导出cookie的json数据,然后自行使用python来包装

@stakeswky
Copy link

真奇怪,我复制cookie进去一直报格式错误:json.decoder.JSONDecodeError: Expecting ',' delimiter: line 6 column 1405 (char 1861)

去浏览器插件商店搜索“cookie editor”,使用这个插件导出cookie的json数据,然后自行使用python来包装

可以了,不过acs-cookie好像是每加载一次就刷新一次,就算是最新的acs-cookie调用的时候也是提示500

@RockChinQ
Copy link

真奇怪,我复制cookie进去一直报格式错误:json.decoder.JSONDecodeError: Expecting ',' delimiter: line 6 column 1405 (char 1861)

去浏览器插件商店搜索“cookie editor”,使用这个插件导出cookie的json数据,然后自行使用python来包装

可以了,不过acs-cookie好像是每加载一次就刷新一次,就算是最新的acs-cookie调用的时候也是提示500

是的,所以难度很大,得破解计算方式

@zhayujie
Copy link
Owner Author

现在这个 acs-token 的过期时间太快了,几分钟就失效了。

只有一边等 API 开放试用, 一边逆向看下 acs-token 在 js 代码中的生成规则是什么

@RockChinQ
Copy link

RockChinQ commented Mar 23, 2023

https://github.com/xw5xr6/revERNIEBot
这里有基于selenium的实现

@Einzieg
Copy link

Einzieg commented Apr 10, 2023

现在这个 acs-token 的过期时间太快了,几分钟就失效了。

只有一边等 API 开放试用, 一边逆向看下 acs-token 在 js 代码中的生成规则是什么

貌似百度开放了文心一言的APIhttps://cloud.baidu.com/doc/WENXINWORKSHOP/s/flfmc9do2

@Ryanyry
Copy link

Ryanyry commented Jul 7, 2023

#!/usr/bin/env python

-- coding: utf-8 --

@time : 2023/7/5 23:32

@author :

@Site :文心一言模型api版本,基于作者的代码改的,我是初学者,看文档没搞定怎么继承对话,只能单独调用,测了微信可用、终端可用

@file : 使用方法:把下面代码复制到yiyan_model.py中替换内容,另外config里面要做对应参数修改即可

@software: PyCharm

from model.model import Model
from config import model_conf
from common import const
from common.log import logger
import requests
import time
import json

sessions = {}

context=[]

#定义一个类,继承model
class YiyanModel(Model):
def init(self):
# self.acs_token = model_conf(const.BAIDU).get('acs_token')
self.client_id = model_conf(const.BAIDU).get('client_id')
self.client_secret = model_conf(const.BAIDU).get('client_secret')
self.base_url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions'
self.access_token=self.get_access_token(self.client_id,self.client_secret)
self.params = {
"access_token": self.access_token
}
#创建函数,获取token
def get_access_token(self,client_id,client_secret):
client_id=str(client_id)
client_secret=str(client_secret)
url = "https://aip.baidubce.com/oauth/2.0/token?client_id=" + client_id + '&client_secret=' + client_secret + '&grant_type=client_credentials'
res = requests.request("POST", url, headers=self._create_header())
res_data = res.json()
access_token = res_data.get('access_token')

    return access_token

#主函数,创建对话获取回复
def reply(self, query, context=None):

    user_id = context.get('session_id') or context.get('from_user_id')
    context['query'] = query
    flag = self.new_chat(context)
    if not flag:
        return "创建会话失败,请稍后再试"

    # 3.query


    # self.query(context, 0, 0)
    messages=  self.query(context, 0, 0)
    return messages

def new_chat(self, context):
    data = {
        "messages": [
            {
                "role": "user",
                "content": "你现在是一个代码专家,帮我处理代码问题"
            }
        ],
        "session_id": "chat_session_id"

    }

    response=requests.post(url=self.base_url, headers=self._create_header(), json=data,params=self.params)
    response=response.json()

    context['messages']=response['result']

    return context

def query(self, context, sentence_id, data):
    data = {"messages": [
                {"role": "user", "content": '你现在是一个代码专家,帮我处理代码问题'},
                {"role": "assistant", "content": context['messages']},
                {"role": "user", "content": context['query']},
            ]


    }
    # print(data)
    response = requests.post(url=self.base_url, headers=self._create_header(), json=data,params=self.params)

    response=response.json()
    # print(response['result'])

    if response['result'] != '':
        # print(context)
    # print(data['messages'])

        data['messages'].append({"role": "assistant", "content": response['result']})
        # print(data['messages'])
    # print({"role": "assistant", "content": response['result']})
    return response['result']
def _create_header(self):
    headers = {'Content-Type': 'application/json;charset=utf-8'}
    return headers

@Ryanyry
Copy link

Ryanyry commented Jul 7, 2023

"baidu": {

  "client_id": "",
  "client_secret": "",
  
},

config.json里面把baidu的文件改成自己的API即可,这个API需要企业申请。

@Ryanyry
Copy link

Ryanyry commented Jul 7, 2023

如果想改代码的朋友,可以联系我,我这有API提供测试

@iamwhatsall
Copy link

如果想改代码的朋友,可以联系我,我这有API提供测试

怎么跟你联系?

@YukiKwok
Copy link

看上面有Python的了,再分享一个node.js的,BAIDU_API_URL是选模型的,ACCESS_TOKEN是千帆申请的就可以一个月有效了。(官方代码给的是request,但我接入用着报错,改成axios了可以正常使用

const axios = require('axios');

const messages = [
{
"role": "user",
"content": content
},
];

try {
const response = await axios.post(${BAIDU_API_URL}?access_token=${ACCESS_TOKEN}, {
"messages": messages
}, {
headers: {
'Content-Type': 'application/json'
}
});

const responseData = response.data;
const responseMessage = responseData.result;

} catch ..........

@Winston916
Copy link

acs_token和cookie都应该填什么呀?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests