-
Notifications
You must be signed in to change notification settings - Fork 277
Changing base url - Usage with open source LLM - Invalid status code: 404 Not Found #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
400 with this: #[tokio::test]
async fn test_stream() {
dotenv().ok();
let messages = match ChatCompletionRequestUserMessageArgs::default()
.content("Write a marketing blog praising and introducing Rust library async-openai")
.build()
{
Ok(msg) => msg.into(),
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
};
let client = Client::with_config(
OpenAIConfig::new()
.with_api_key(&std::env::var("MODEL_API_KEY").unwrap_or_default())
.with_api_base("https://api.perplexity.ai"),
);
let request = match CreateChatCompletionRequestArgs::default()
.model("mistralai/mixtral-8x7b-instruct")
.max_tokens(512u16)
.messages([messages])
.build()
{
Ok(req) => req,
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
};
let stream_result = client.chat().create_stream(request).await;
let mut stream = match stream_result {
Ok(s) => s,
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
};
let mut lock = stdout().lock();
while let Some(result) = stream.next().await {
match result {
Ok(response) => {
response.choices.iter().for_each(|chat_choice| {
if let Some(ref content) = chat_choice.delta.content {
write!(lock, "{}", content).unwrap();
}
});
}
Err(err) => {
writeln!(lock, "error: {err}").unwrap();
}
}
match stdout().flush() {
Ok(_) => (),
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
}
}
} |
interesting, using mistral api, different results: #[tokio::test]
async fn test_stream() {
dotenv().ok();
let messages = match ChatCompletionRequestUserMessageArgs::default()
.content("Write a marketing blog praising and introducing Rust library async-openai")
.build()
{
Ok(msg) => msg.into(),
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
};
let client = Client::with_config(
OpenAIConfig::new()
.with_api_key(&std::env::var("MODEL_API_KEY").unwrap_or_default())
.with_api_base("https://api.mistral.ai/v1"),
);
let request = match CreateChatCompletionRequestArgs::default()
// .model("mistralai/mixtral-8x7b-instruct")
.model("mistral-tiny")
.max_tokens(512u16)
.messages([messages])
.build()
{
Ok(req) => req,
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
};
let stream_result = client.chat().create_stream(request).await;
let mut stream = match stream_result {
Ok(s) => s,
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
};
let mut lock = stdout().lock();
while let Some(result) = stream.next().await {
match result {
Ok(response) => {
response.choices.iter().for_each(|chat_choice| {
if let Some(ref content) = chat_choice.delta.content {
write!(lock, "{}", content).unwrap();
}
});
}
Err(err) => {
println!("Error: {}", err);
// jsonify error
let err = json!({
"error": err.to_string()
});
println!("error: {}", err);
writeln!(lock, "error: {err}").unwrap();
}
}
match stdout().flush() {
Ok(_) => (),
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
}
}
}
|
I think this isn't out of scope anymore since #191 depends on it. |
This seems to work based on @louis030195's code using use std::io::{stdout, Write};
use async_openai::types::{ChatCompletionRequestUserMessageArgs, CreateChatCompletionRequestArgs};
use async_openai::config::OpenAIConfig;
use llm_chain::output::StreamExt;
use async_openai::Client;
use serde_json::json;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
let api_key = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set");
let api_host = "http://localhost:11434/v1";
let api_model = "llama3:latest";
let max_tokens = 512u16;
let messages = match ChatCompletionRequestUserMessageArgs::default()
.content("Write a marketing blog praising and introducing Rust library async-openai")
.build()
{
Ok(msg) => msg.into(),
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
};
let client = Client::with_config(
OpenAIConfig::new()
.with_api_key(&api_key)
.with_api_base(api_host)
);
let request = match CreateChatCompletionRequestArgs::default()
.model(api_model)
.max_tokens(max_tokens)
.messages([messages])
.build()
{
Ok(req) => req,
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
};
let stream_result = client.chat().create_stream(request).await;
let mut stream = match stream_result {
Ok(s) => s,
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
};
let mut lock = stdout().lock();
while let Some(result) = stream.next().await {
match result {
Ok(response) => {
response.choices.iter().for_each(|chat_choice| {
if let Some(ref content) = chat_choice.delta.content {
write!(lock, "{}", content).unwrap();
}
});
}
Err(err) => {
println!("Error: {}", err);
// jsonify error
let err = json!({
"error": err.to_string()
});
println!("error: {}", err);
writeln!(lock, "error: {err}").unwrap();
}
}
match stdout().flush() {
Ok(_) => (),
Err(e) => {
println!("Error: {}", e);
assert!(false);
return;
}
}
}
} |
for anyone bumping into this, you only need to drop the last |
Hey I'm trying to use
async-openai
withaxum
and open source LLMs throughperplexity.ai
in my test.Basically my endpoint would route the request to OpenAI API or an OpenAI API like API changing the URL of the API based on the given model like
gpt-4
would go to openai andmistralai/whatever
would go toMODEL_URL
Getting a 404. Not sure if I'm doing something wrong or this use case is implemented?
These are my env var
My code:
Any help appreciated 🙏
The text was updated successfully, but these errors were encountered: