Skip to content

Commit 8dab6af

Browse files
committed
update example
1 parent eeedb94 commit 8dab6af

File tree

7 files changed

+124
-112
lines changed

7 files changed

+124
-112
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
[package]
2-
name = "openai-web-app"
2+
name = "openai-web-app-chat"
33
version = "0.1.0"
44
edition = "2021"
55
publish = false
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
dioxus = "~0.4"
11-
dioxus-web = "~0.4"
12-
futures = "0.3.28"
10+
dioxus = {version = "~0.5", features = ["web"]}
11+
futures = "0.3.30"
1312
async-openai-wasm = { path = "../../async-openai-wasm" }
14-
console_log = "1.0"
15-
log = "0.4"
13+
# Debug
14+
tracing = "0.1.40"
15+
dioxus-logger = "~0.5"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[application]
2+
3+
# App (Project) Name
4+
name = "openai-web-app-chat-dioxus"
5+
6+
# Dioxus App Default Platform
7+
# desktop, web
8+
default_platform = "web"
9+
10+
# `build` & `serve` dist path
11+
out_dir = "dist"
12+
13+
[web.app]
14+
15+
# HTML title tag content
16+
title = "openai-web-app-chat-dioxus"
17+
18+
[web.watcher]
19+
20+
# when watcher trigger, regenerate the `index.html`
21+
reload_html = true
22+
23+
# which files or dirs will be watcher monitoring
24+
watch_path = ["src"]
25+
26+
# include `assets` in web platform
27+
[web.resource]
28+
29+
# CSS style file
30+
31+
style = []
32+
33+
# Javascript code file
34+
script = []
35+
36+
[web.resource.dev]
37+
38+
# Javascript code file
39+
# serve: [dev-server] only
40+
script = []
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# OpenAI Web App - Chat
2+
3+
This builds a `dioxus` web App that uses OpenAI ChatCompletion APIs to generate text.
4+
5+
To run it, you need:
6+
1. Set OpenAI secrets in `./src/main.rs`. Please do NOT take this demo into production without using a secure secret store
7+
2. Install `dioxus-cli` by `cargo install dioxus-cli`.
8+
3. Run `dx serve`
9+
10+
Note: Safari may not work due to CORS issues. Please use Chrome or Edge.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#![allow(non_snake_case)]
2+
3+
use dioxus::prelude::*;
4+
use dioxus_logger::tracing::{Level, info, error};
5+
use futures::stream::StreamExt;
6+
use async_openai_wasm::Client;
7+
use async_openai_wasm::config::OpenAIConfig;
8+
use async_openai_wasm::types::{ChatCompletionRequestMessage, ChatCompletionRequestUserMessageArgs, CreateChatCompletionRequestArgs};
9+
10+
11+
const API_BASE: &str = "...";
12+
const API_KEY: &str = "...";
13+
14+
pub fn App() -> Element {
15+
const GREETING: &str = "Hello! How are you?";
16+
let request = CreateChatCompletionRequestArgs::default()
17+
.max_tokens(512u16)
18+
.model("gpt-3.5-turbo")
19+
.messages([
20+
ChatCompletionRequestMessage::User(
21+
ChatCompletionRequestUserMessageArgs::default()
22+
.content(GREETING)
23+
.build()
24+
.unwrap()
25+
)
26+
])
27+
.build().unwrap();
28+
let response_string = use_signal(String::new);
29+
let _fetch_completion_chunks: Coroutine<()> = use_coroutine(|_rx| {
30+
let mut response_string = response_string.to_owned();
31+
async move {
32+
let config = OpenAIConfig::new()
33+
.with_api_key(API_KEY);
34+
let config = if API_BASE != "..." {
35+
config.with_api_base(API_BASE)
36+
} else {
37+
config
38+
};
39+
let client = Client::with_config(config);
40+
let mut stream = client.chat().create_stream(request).await.unwrap();
41+
while let Some(chunk) = stream.next().await {
42+
match chunk {
43+
Ok(response) =>
44+
response_string.with_mut(|string| {
45+
if let Some(content) = response.choices[0].delta.content.as_ref() {
46+
info!("Response chunk: {:?}", content);
47+
string.push_str(content);
48+
}
49+
}),
50+
Err(e) => error!("OpenAI Error: {:?}", e)
51+
}
52+
}
53+
}
54+
});
55+
56+
rsx! {
57+
div {
58+
p { "Using OpenAI" }
59+
p { "User: {GREETING}" }
60+
p { "Assistant: {response_string}" }
61+
}
62+
}
63+
}
64+
65+
fn main() {
66+
dioxus_logger::init(Level::INFO).expect("failed to init logger");
67+
launch(App);
68+
}

examples/openai-web-app/README.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/openai-web-app/src/main.rs

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)