GithubHelp home page GithubHelp logo

Comments (3)

louis030195 avatar louis030195 commented on September 22, 2024

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;
            }
        }
    }
}

from async-openai.

louis030195 avatar louis030195 commented on September 22, 2024

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;
            }
        }
    }
}

running 1 test
Error: failed to deserialize api response: missing field created at line 1 column 154
error: {"error":"failed to deserialize api response: missing field created at line 1 column 154"}
error: {"error":"failed to deserialize api response: missing field created at line 1 column 154"}
Title: Unleashing Creativity and Productivity: An Introduction to async-openai, the Game-Changing Rust Library for Interacting with OpenAI

As technology continues to evolve at an unprecedented pace, developers are constantly on the lookout for tools that can help them build applications faster, more efficiently, and with greater creativity. One such tool that has been generating buzz in the Rust community is async-openai, an innovative library for interacting with OpenAI's powerful language models. In this blog post, we'll explore why async-openai is a must-have addition to any Rust developer's toolkit and how it can help you build AI-powered applications with ease.

First, let's talk about OpenAI. OpenAI is a leading artificial intelligence research laboratory, and its language models, such as the popular ChatGPT, have captured the imagination of people around the world with their ability to generate human-like text based on given prompts. Interacting with OpenAI's models, however, can be a complex and time-consuming process, especially for those who want to build applications that can leverage these models in real-time. This is where async-openai comes in.

async-openai is an asynchronous Rust library for interacting with OpenAI's models. Asynchronous programming is a programming paradigm that allows multiple tasks to be executed concurrently, which is essential for building high-performance applications. By using async-openai, Rust developers can easily integrate OpenAI's models into their applications, enabling real-time, responsive interactions with the models.

One of the key benefits of using async-openai is its simplicity. The library provides a straightforward and intuitive API for sending requests to OpenAI's models

from async-openai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.