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!
// 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!