forked from 64bit/async-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix stream object * Add assistant streaming + func call example * Fix old OpenAI chat example
- Loading branch information
Showing
15 changed files
with
533 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "openai-web-assistant-chat" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
dioxus = {version = "~0.5", features = ["web"]} | ||
futures = "0.3.30" | ||
async-openai-wasm = { path = "../../async-openai-wasm" } | ||
# Debug | ||
tracing = "0.1.40" | ||
dioxus-logger = "~0.5" | ||
serde_json = "1.0.117" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
[application] | ||
|
||
# App (Project) Name | ||
name = "openai-web-app-assistant-dioxus" | ||
|
||
# Dioxus App Default Platform | ||
# desktop, web | ||
default_platform = "web" | ||
|
||
# `build` & `serve` dist path | ||
out_dir = "dist" | ||
|
||
[web.app] | ||
|
||
# HTML title tag content | ||
title = "openai-web-app-assistant-dioxus" | ||
|
||
[web.watcher] | ||
|
||
# when watcher trigger, regenerate the `index.html` | ||
reload_html = true | ||
|
||
# which files or dirs will be watcher monitoring | ||
watch_path = ["src"] | ||
|
||
# include `assets` in web platform | ||
[web.resource] | ||
|
||
# CSS style file | ||
|
||
style = [] | ||
|
||
# Javascript code file | ||
script = [] | ||
|
||
[web.resource.dev] | ||
|
||
# Javascript code file | ||
# serve: [dev-server] only | ||
script = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# OpenAI Web App - Assistant | ||
|
||
This builds a `dioxus` web App that uses OpenAI Assistant APIs to generate text. | ||
|
||
To run it, you need: | ||
1. Set OpenAI secrets in `./src/main.rs`. Please do NOT take this demo into production without using a secure secret store | ||
2. Install `dioxus-cli` by `cargo install dioxus-cli`. | ||
3. Run `dx serve` | ||
|
||
Note: Safari may not work due to CORS issues. Please use Chrome or Edge. | ||
|
||
## Reference | ||
|
||
The code is adapted from [assistant-func-call-stream example in async-openai](https://github.com/64bit/async-openai/tree/main/examples/assistants-func-call-stream). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#![allow(non_snake_case)] | ||
|
||
use dioxus::prelude::*; | ||
use dioxus_logger::tracing::{error, info, Level}; | ||
use futures::stream::StreamExt; | ||
|
||
use async_openai_wasm::types::{AssistantStreamEvent, CreateMessageRequest, CreateRunRequest, CreateThreadRequest, MessageRole}; | ||
|
||
use crate::utils::*; | ||
|
||
mod utils; | ||
|
||
pub const API_BASE: &str = "..."; | ||
pub const API_KEY: &str = "..."; | ||
|
||
|
||
pub fn App() -> Element { | ||
const QUERY: &str = "What's the weather in San Francisco today and the likelihood it'll rain?"; | ||
let reply = use_signal(String::new); | ||
let _run_assistant: Coroutine<()> = use_coroutine(|_rx| { | ||
let client = get_client(); | ||
async move { | ||
// | ||
// Step 1: Define functions | ||
// | ||
let assistant = client | ||
.assistants() | ||
.create(create_assistant_request()) | ||
.await | ||
.expect("failed to create assistant"); | ||
// | ||
// Step 2: Create a Thread and add Messages | ||
// | ||
let thread = client | ||
.threads() | ||
.create(CreateThreadRequest::default()) | ||
.await | ||
.expect("failed to create thread"); | ||
let _message = client | ||
.threads() | ||
.messages(&thread.id) | ||
.create(CreateMessageRequest { | ||
role: MessageRole::User, | ||
content: QUERY.into(), | ||
..Default::default() | ||
}) | ||
.await | ||
.expect("failed to create message"); | ||
// | ||
// Step 3: Initiate a Run | ||
// | ||
let mut event_stream = client | ||
.threads() | ||
.runs(&thread.id) | ||
.create_stream(CreateRunRequest { | ||
assistant_id: assistant.id.clone(), | ||
stream: Some(true), | ||
..Default::default() | ||
}) | ||
.await | ||
.expect("failed to create run"); | ||
|
||
|
||
while let Some(event) = event_stream.next().await { | ||
match event { | ||
Ok(event) => match event { | ||
AssistantStreamEvent::ThreadRunRequiresAction(run_object) => { | ||
info!("thread.run.requires_action: run_id:{}", run_object.id); | ||
handle_requires_action(&client, run_object, reply.to_owned()).await | ||
} | ||
_ => info!("\nEvent: {event:?}\n"), | ||
}, | ||
Err(e) => { | ||
error!("Error: {e}"); | ||
} | ||
} | ||
} | ||
|
||
client.threads().delete(&thread.id).await.expect("failed to delete thread"); | ||
client.assistants().delete(&assistant.id).await.expect("failed to delete assistant"); | ||
info!("Done!"); | ||
} | ||
}); | ||
|
||
rsx! { | ||
div { | ||
p { "Using OpenAI" } | ||
p { "User: {QUERY}" } | ||
p { "Expected Stats (Debug): temperature = {TEMPERATURE}, rain_probability = {RAIN_PROBABILITY}" } | ||
p { "Assistant: {reply}" } | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
dioxus_logger::init(Level::INFO).expect("failed to init logger"); | ||
launch(App); | ||
} |
Oops, something went wrong.