WeLM是微信推出的一个能够理解和生成文本的通用语言模型(类似ChatGPT),提供了API来解决各种文本任务。在本教程中,我们将展示如何利用PHP和Python来接入微信AI语言模型WeLM。
使用方法
一、申请微信AI·WELM密钥
1、打开官网,填写问卷,会自动发送Key到邮箱,点击下面按钮直达
2、问卷最好不要使用QQ邮箱,使用其他邮箱(其他邮箱几分钟就发放Token了,qq邮箱一直都没收到token发放记录)
二、PHP对接微信AI-Welm
1、不废话直接上代码(第45行换成你们在welm官网申请的key)
<?phpfunction query_api($prompt, $model, $max_tokens, $temperature, $top_p, $top_k, $n, $echo, $stop, $token) {/** * Welm 微信AI * Author:领酷网 */$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 requestsimport 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)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)