feat: add chain method to create Request instance

This commit is contained in:
Night Cruising
2023-10-17 15:14:31 +08:00
parent f1f1fa7e86
commit a9be9efdfe
16 changed files with 402 additions and 135 deletions

View File

@ -4,29 +4,20 @@ use std::env;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = ChatCompletionRequest {
model: chat_completion::GPT4.to_string(),
messages: vec![chat_completion::ChatCompletionMessage {
let req = ChatCompletionRequest::new(
chat_completion::GPT4.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: String::from("What is Bitcoin?"),
name: None,
function_call: None,
}],
functions: None,
function_call: None,
temperature: None,
top_p: None,
n: None,
stream: None,
stop: None,
max_tokens: None,
presence_penalty: None,
frequency_penalty: None,
logit_bias: None,
user: None,
};
);
let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
Ok(())
}

View File

@ -4,24 +4,18 @@ use std::env;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = CompletionRequest {
model: completion::GPT3_TEXT_DAVINCI_003.to_string(),
prompt: String::from("What is Bitcoin?"),
suffix: None,
max_tokens: Some(3000),
temperature: Some(0.9),
top_p: Some(1.0),
n: None,
stream: None,
logprobs: None,
echo: None,
stop: Some(vec![String::from(" Human:"), String::from(" AI:")]),
presence_penalty: Some(0.6),
frequency_penalty: Some(0.0),
best_of: None,
logit_bias: None,
user: None,
};
let req = CompletionRequest::new(
completion::GPT3_TEXT_DAVINCI_003.to_string(),
String::from("What is Bitcoin?"),
)
.max_tokens(3000)
.temperature(0.9)
.top_p(1.0)
.stop(vec![String::from(" Human:"), String::from(" AI:")])
.presence_penalty(0.6)
.frequency_penalty(0.0);
let result = client.completion(req)?;
println!("{:}", result.choices[0].text);

View File

@ -4,11 +4,12 @@ use std::env;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = EmbeddingRequest {
model: "text-embedding-ada-002".to_string(),
input: "story time".to_string(),
user: Option::None,
};
let req = EmbeddingRequest::new(
"text-embedding-ada-002".to_string(),
"story time".to_string(),
);
let result = client.embedding(req)?;
println!("{:?}", result.data);

View File

@ -29,35 +29,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}),
);
let req = ChatCompletionRequest {
model: chat_completion::GPT3_5_TURBO_0613.to_string(),
messages: vec![chat_completion::ChatCompletionMessage {
let req = ChatCompletionRequest::new(
chat_completion::GPT3_5_TURBO_0613.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: String::from("What is the price of Ethereum?"),
name: None,
function_call: None,
}],
functions: Some(vec![chat_completion::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: chat_completion::FunctionParameters {
schema_type: chat_completion::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
}]),
function_call: Some(FunctionCallType::Auto), // Some(FunctionCallType::Function { name: "test".to_string() }),
temperature: None,
top_p: None,
n: None,
stream: None,
stop: None,
max_tokens: None,
presence_penalty: None,
frequency_penalty: None,
logit_bias: None,
user: None,
};
)
.functions(vec![chat_completion::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: chat_completion::FunctionParameters {
schema_type: chat_completion::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
}])
.function_call(FunctionCallType::Auto);
// debug reuqest json
// let serialized = serde_json::to_string(&req).unwrap();

View File

@ -29,35 +29,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}),
);
let req = ChatCompletionRequest {
model: chat_completion::GPT3_5_TURBO_0613.to_string(),
messages: vec![chat_completion::ChatCompletionMessage {
let req = ChatCompletionRequest::new(
chat_completion::GPT3_5_TURBO_0613.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: String::from("What is the price of Ethereum?"),
name: None,
function_call: None,
}],
functions: Some(vec![chat_completion::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: chat_completion::FunctionParameters {
schema_type: chat_completion::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
}]),
function_call: None,
temperature: None,
top_p: None,
n: None,
stream: None,
stop: None,
max_tokens: None,
presence_penalty: None,
frequency_penalty: None,
logit_bias: None,
user: None,
};
)
.functions(vec![chat_completion::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: chat_completion::FunctionParameters {
schema_type: chat_completion::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
}]);
let result = client.chat_completion(req)?;
@ -80,9 +69,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
let req = ChatCompletionRequest {
model: chat_completion::GPT3_5_TURBO_0613.to_string(),
messages: vec![
let req = ChatCompletionRequest::new(
chat_completion::GPT3_5_TURBO_0613.to_string(),
vec![
chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: String::from("What is the price of Ethereum?"),
@ -99,19 +88,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
function_call: None,
},
],
functions: None,
function_call: None,
temperature: None,
top_p: None,
n: None,
stream: None,
stop: None,
max_tokens: None,
presence_penalty: None,
frequency_penalty: None,
logit_bias: None,
user: None,
};
);
let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
}