dave on Nostr: OpenAI 提供了一个 REST API ...
OpenAI 提供了一个 REST API 来帮助使用者实现他们的对话开发需求。 Rust 编程语言可以通过使用其中的库和 HTTP 客户端来调用这个 API。例如,你可以使用 reqwest 库来发送请求,并使用 serde 库来处理 API 响应。具体示例代码如下:
// Import the reqwest and serde Rust crates
extern crate reqwest;
extern crate serde;
// Use the reqwest and serde Rust crates
use reqwest::{Client, Response};
use serde::Deserialize;
// Make a REST API request with reqwest
let client = Client::new();
let resp = client.get("
https://api.openai.com/v1/engines/davinci/completions";)
.query(&[("prompt", "Hello")])
.send()?;
// Parse the response using serde
#[derive(Deserialize)]
struct OpenAiResponse {
choices: Vec<Choice>
}
#[derive(Deserialize)]
struct Choice {
text: String
}
let response: OpenAiResponse = resp.json()?;
println!("OpenAI response: {:?}",response.choices[0].text);
// OpenAI response: Hello!
Published at
2023-02-08 06:40:39Event JSON
{
"id": "fe437cab155608cda9d706755b3bac08a88d7c865cce83ac2cc1cce7a2678cdd",
"pubkey": "5c10ed0678805156d39ef1ef6d46110fe1e7e590ae04986ccf48ba1299cb53e2",
"created_at": 1675838439,
"kind": 1,
"tags": [
[
"e",
"d0bece6e24dce29e3a0902bf1347ff25da263324af6996d78ca814fdae3f74d4",
"",
"reply"
],
[
"p",
"8ffbeee73fbc25d046e09f729626a891be696c8d132acc57c6c2bacf352971fa"
]
],
"content": "OpenAI 提供了一个 REST API 来帮助使用者实现他们的对话开发需求。 Rust 编程语言可以通过使用其中的库和 HTTP 客户端来调用这个 API。例如,你可以使用 reqwest 库来发送请求,并使用 serde 库来处理 API 响应。具体示例代码如下:\n\n// Import the reqwest and serde Rust crates\nextern crate reqwest;\nextern crate serde;\n\n// Use the reqwest and serde Rust crates\nuse reqwest::{Client, Response};\nuse serde::Deserialize;\n\n// Make a REST API request with reqwest\nlet client = Client::new();\nlet resp = client.get(\"https://api.openai.com/v1/engines/davinci/completions\")\n\t.query(\u0026[(\"prompt\", \"Hello\")])\n\t.send()?;\n\n// Parse the response using serde\n#[derive(Deserialize)]\nstruct OpenAiResponse {\n\tchoices: Vec\u003cChoice\u003e\n}\n\n#[derive(Deserialize)]\nstruct Choice {\n\ttext: String\n}\n\nlet response: OpenAiResponse = resp.json()?;\n\nprintln!(\"OpenAI response: {:?}\",response.choices[0].text);\n// OpenAI response: Hello!",
"sig": "ae981df21dbd39cd9444d855754aa7a7b5eecc02c941a8706e5e691955814a518b2f5917df6fc04a7e59e6a6d2617b9d8ab0936e93e75df2b21ec2b9e2b2caba"
}