33 releases (8 breaking)

0.9.1 Nov 7, 2023
0.8.5 Jul 5, 2023
0.2.3 Mar 29, 2023

#6 in #flows

Download history 25/week @ 2024-01-06 32/week @ 2024-01-13 10/week @ 2024-01-20 8/week @ 2024-01-27 14/week @ 2024-02-10 309/week @ 2024-02-17 141/week @ 2024-02-24 16/week @ 2024-03-02 22/week @ 2024-03-09 19/week @ 2024-03-16 95/week @ 2024-03-30 35/week @ 2024-04-06 60/week @ 2024-04-13 122/week @ 2024-04-20

312 downloads per month

MIT/Apache

32KB
496 lines

This is a library for integrating OpenAI in your flow function for flows.network.

Visit ChatGPT

use flowsnet_platform_sdk::logger;
use lambda_flows::{request_received, send_response};
use openai_flows::{
    chat::{ChatModel, ChatOptions},
    OpenAIFlows,
};
use serde_json::Value;
use std::collections::HashMap;

#[no_mangle]
#[tokio::main(flavor = "current_thread")]
pub async fn run() {
    logger::init();
    request_received(handler).await;
}

async fn handler(_qry: HashMap<String, Value>, body: Vec<u8>) {
    let co = ChatOptions {
        model: ChatModel::GPT35Turbo,
        restart: false,
        system_prompt: None,
    };
    let of = OpenAIFlows::new();

    let r = match of
        .chat_completion(
            "any_conversation_id",
            String::from_utf8_lossy(&body).into_owned().as_str(),
            &co,
        )
        .await
    {
        Ok(c) => c.choice,
        Err(e) => e,
    };

    send_response(
        200,
        vec![(
            String::from("content-type"),
            String::from("text/plain; charset=UTF-8"),
        )],
        r.as_bytes().to_vec(),
    );
}

This example lets you have a conversation with ChatGPT using chat_completion by Lambda request.

The whole document is here.

Dependencies

~1–1.8MB
~42K SLoC