前言说明
WeLM是微信推出的一个能够理解和生成文本的通用语言模型(类似ChatGPT),提供了API来解决各种文本任务。在本教程中,我们将展示如何利用PHP和Python来接入微信AI语言模型WeLM。
使用方法
一、申请微信AI·WELM密钥
1、打开官网,填写问卷,会自动发送Key到邮箱,点击下面按钮直达
2、问卷最好不要使用QQ邮箱,使用其他邮箱(其他邮箱几分钟就发放Token了,qq邮箱一直都没收到token发放记录)
二、PHP对接微信AI-Welm
1、不废话直接上代码(第45行换成你们在welm官网申请的key)
<?php
function query_api($prompt, $model, $max_tokens, $temperature, $top_p, $top_k, $n, $echo, $stop, $token) {
/**
* Welm 微信AI
* Author:360模板吧
*/
$url = "https://welm.weixin.qq.com/v1/completions";
$data = array(
"prompt" => $prompt,
"model" => $model,
"max_tokens" => $max_tokens,
"temperature" => $temperature,
"top_p" => $top_p,
"top_k" => $top_k,
"n" => $n,
"echo" => $echo,
"stop" => $stop
);
$data = json_encode($data);
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer " . $token
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "请求失败: " . $err;
return null;
} else {
$result = json_decode($response, true);
return $result;
}
}
if (isset($_POST["submit"])) {
$prompt = "测试";
$model = "xl";
$max_tokens = 16;
$temperature = 0.0;
$top_p = 0.0;
$top_k = 10;
$n = 1;
$echo = false;
$stop = ",,.。";
$token = "你的token";
$result = query_api($prompt, $model, $max_tokens, $temperature, $top_p, $top_k, $n, $echo, $stop, $token);
if ($result) {
echo "<pre>";
print_r($result);
echo "</pre>";
}
}
?>
<form method="post">
<input type="submit" name="submit" value="获取结果">
</form>
三、Python对接微信AI-Welm
1、不废话直接上代码,直接上代码(第45行换成你们在welm官网申请的key)
import requests
import json
def query_api(prompt, model, max_tokens, temperature, top_p, top_k, n, echo, stop, token):
"""
查询API接口
"""
url = 'https://welm.weixin.qq.com/v1/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}
data = {
"prompt": prompt,
"model": model,
"max_tokens": max_tokens,
"temperature": temperature,
"top_p": top_p,
"top_k": top_k,
"n": n,
"echo": echo,
"stop": stop
}
try:
response = requests.post(url, headers=headers, data=json.dumps(data))
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print(f"请求失败: {err}")
return None
result = json.loads(response.text)
return result
if __name__ == '__main__':
prompt = "测试"
model = "xl"
max_tokens = 16
temperature = 0.0
top_p = 0.0
top_k = 10
n = 1
echo = False
stop = ",,.。"
token = "你的token"
result = query_api(prompt, model, max_tokens, temperature, top_p, top_k, n, echo, stop, token)
if result:
print(result)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END