wordpress對應國家語言百度站長工具seo查詢
一、什么是智譜AI
智譜AI成立于2019年,由?清華大學計算機系知識工程實驗室的技術(shù)成果轉(zhuǎn)化而來,是一家致力于人工智能技術(shù)研發(fā)和應用的公司。智譜致力于打造新一代認知智能大模型,專注于做大模型的中國創(chuàng)新。
?
二、智譜開放平臺API調(diào)用
官方文檔
https://open.bigmodel.cn/dev/api#http_para
?
創(chuàng)建應用
https://open.bigmodel.cn/usercenter/apikeys
?
?
Nodejs Http調(diào)用示例?
cnpm i request --save
const request = require('request')
async function main() {
let url = "https://open.bigmodel.cn/api/paas/v4/chat/completions"
let body = {
"model": "glm-4", // 模型選擇
"temperature": 0.9, //核采樣閾值,用于決定結(jié)果隨機性,取值越高隨機性越強,即相同
的問題得到的不同答案的可能性越高。取值范圍 (0,1)
"top-k": 4, //平衡生成文本的質(zhì)量和多樣性,較小的 k 值會減少隨機性,使得輸
出更加穩(wěn)定;而較大的 k 值會增加隨機性,產(chǎn)生更多新穎的輸出。取值范圍[1, 6],默認為4
"max_tokens": 1000, //模型回答的tokens的最大長度
"messages": [
{
"role": "system", //用于設(shè)置對話背景,角色設(shè)定
"content": "你是一個聰明且富有創(chuàng)造力的小說作家"
},
{
"role": "user",
"content": "你是誰"
}
],
}
header = {
"Authorization": "Bearer
8e67b3159ae070a71d71fe97fceb733b.Bf7fRh7Yy7uWxdDp", // 注意此處替換自己的key和
secret
'Content-Type': 'application/json'
}
var options = {
'method': 'POST',
'url': url,
'headers': header,
body: JSON.stringify(body)
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
}
main();
Golang Http調(diào)用示例?
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
url := "https://open.bigmodel.cn/api/paas/v4/chat/completions"
body := map[string]interface{}{
"model": "glm-4",
"temperature": 0.9,
"top-k": 4,
"max_tokens": 100,
"messages": []map[string]interface{}{
{
"role": "system",
"content": "你是一個聰明且富有創(chuàng)造力的小說作家",
},
{
"role": "user",
"content": "你是誰",
},
},
}
jsonBody, err := json.Marshal(body)
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
req.Header.Set("Authorization", "Bearer
8e67b3159ae070a71d71fe97fceb733b.Bf7fRh7Yy7uWxdDp")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
}
defer resp.Body.Close()
var result map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
fmt.Println("Error decoding response:", err)
return
}
fmt.Println(result)
}
?Python Http調(diào)用示例
import requests
import json
# 定義請求的URL
url = "https://open.bigmodel.cn/api/paas/v4/chat/completions"
# 定義請求的body
body = {
"model": "glm-4",
"temperature": 0.9,
"top-k": 4,
"max_tokens": 100,
"messages": [
{
"role": "system",
"content": "你是一個聰明且富有創(chuàng)造力的小說作家"
},
{
"role": "user",
"content": "你是誰"
}
],
}
# 定義請求的headers
headers = {
"Authorization": "Bearer
8e67b3159ae070a71d71fe97fceb733b.Bf7fRh7Yy7uWxdDp",
'Content-Type': 'application/json'
}
# 發(fā)送POST請求
response = requests.post(url, headers=headers, data=json.dumps(body))
# 打印響應結(jié)果
print(response.json())
OpenAi調(diào)用示例 ?
https://github.com/openai/openai-node
?
https://github.com/openai/openai-python
?
https://github.com/sashabaranov/go-openai
?
官方文檔 調(diào)用示例:
?
https://www.xfyun.cn/doc/spark/HTTP%E8%B0%83%E7%94%A8%E6%96%87%E6%A1%A3.html#_7
-%E4%BD%BF%E7%94%A8openai-sdk%E8%AF%B7%E6%B1%82%E7%A4%BA%E4%BE%8B
?
Python openAi
?
https://github.com/openai/openai-python
?
1、安裝依賴?
pip install openai
2、請求?
# https://github.com/openai/openai-python
# pip install openai
from openai import OpenAI
client = OpenAI(
api_key="8e67b3159ae070a71d71fe97fceb733b.Bf7fRh7Yy7uWxdDp",
base_url="https://open.bigmodel.cn/api/paas/v4/"
)
completion = client.chat.completions.create(
model="glm-4",
messages=[
{"role": "system", "content": "你是一個聰明且富有創(chuàng)造力的小說作家"},
{"role": "user", "content": "請你作為童話故事大王,寫一篇短篇童話故事,故事的主
題是要永遠保持一顆善良的心,要能夠激發(fā)兒童的學習興趣和想象力,同時也能夠幫助兒童更好地理解和接
受故事中所蘊含的道理和價值觀。"}
],
top_p=0.7,
temperature=0.9
)
print(completion.choices[0].message)
Nodejs openAi
?
https://github.com/openai/openai-node
?
安裝依賴
?
npm install openai --save
cnpm install openai --save
使用import 需要在package.json里面需要配置"type":"module", ?
{
"type": "module",
"dependencies": {
}
}
配置代碼 ?
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: "8e67b3159ae070a71d71fe97fceb733b.Bf7fRh7Yy7uWxdDp",
baseURL: "https://open.bigmodel.cn/api/paas/v4/"
});
async function main() {
const chatCompletion = await client.chat.completions.create({
messages: [{ role: 'user', content: '你好' }],
model: 'GLM-4-Flash',
});
console.log(chatCompletion.choices[0].message.content);
}
main();
?
智譜開放平臺API調(diào)用視頻詳解:
【應用開發(fā)】分別用nodejs python golang Opne Ai調(diào)用 ChatGLM 智譜AI大模型的Api
?
?
?
?
?
?
?
?
?
?
?
?
?