Skip to content

Commit

Permalink
benchmark hono-openai example
Browse files Browse the repository at this point in the history
  • Loading branch information
redoC-A2k committed Apr 21, 2024
1 parent da99cbf commit 3e2b01e
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 1 deletion.
8 changes: 8 additions & 0 deletions JS/wasm/benchmark/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const autocannon = require('autocannon');

autocannon({
url:"http://localhost:8080/quote?query=fun",
connections: 100,
pipelining: 1,
duration:10
},console.log)
15 changes: 15 additions & 0 deletions JS/wasm/benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "benchmark",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"autocannon": "^7.15.0"
}
}
2 changes: 1 addition & 1 deletion JS/wasm/crates/serve/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub fn add_jsonnet_to_linker(linker: &mut Linker<WasiCtx>) -> anyhow::Result<()>
value.as_str().expect("ext_string value is not a string"),
);
}
let code = fs::read_to_string(path).expect("File not found");
let code = fs::read_to_string(path).expect(&format!("File not found {}",path));
let out = jsonnet_evaluate_snippet(vm, "deleteme", &code);
let mut output: std::sync::MutexGuard<'_, String> = output.lock().unwrap();
*output = out;
Expand Down
1 change: 1 addition & 0 deletions JS/wasm/examples/hono-openai/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/
26 changes: 26 additions & 0 deletions JS/wasm/examples/hono-openai/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { build } from "esbuild";
let runtime = process.argv[2];


build({
entryPoints: ["src/index.js", "src/prompts/quote.jsonnet","src/prompts/config.jsonnet"],
// entryPoints: ["src/index.js"],
bundle: true,
// minify: true,
minifySyntax: true,
// outfile: "bin/app.js",
outdir: "bin",
format: "esm",
target: "esnext",
platform: "node",
// external: ["arakoo"],
loader:{
".jsonnet":"copy"
},
define: {
"process.env.arakoo": JSON.stringify(runtime === "arakoo"),
},
}).catch((error) => {
console.error(error);
process.exit(1);
});
21 changes: 21 additions & 0 deletions JS/wasm/examples/hono-openai/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "hono-openai",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@arakoodev/jsonnet": "file:../../../jsonnet/",
"@hono/node-server": "^1.11.0",
"hono": "^3.9"
},
"type": "module",
"devDependencies": {
"esbuild": "^0.20.2"
}
}
56 changes: 56 additions & 0 deletions JS/wasm/examples/hono-openai/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Hono } from "hono";
const app = new Hono();
// import { serve } from "@hono/node-server";
import Jsonnet from "@arakoodev/jsonnet";

let openAiEndpoint = "https://api.openai.com/v1/chat/completions";

app.get('/quote', async (c) => {
try {
let jsonnet = new Jsonnet();
let query = c.req.query("query");
let prompt = JSON.parse(jsonnet.evaluateFile("./prompts/quote.jsonnet")).prompt;
let fetchBody = jsonnet.extString("prompt", prompt).extString("query", query).extString("model", "gpt-3.5-turbo").evaluateFile("./prompts/config.jsonnet");
console.log(typeof fetchBody)
let response = await fetch(openAiEndpoint, {
method: "POST",
headers: {
"Content-type": "application/json",
"Authorization": "Bearer openai key here"
},
body: fetchBody
});
let body = await response.json();
console.log(body.choices[0].message.content)
return c.json(body.choices[0].message.content);
} catch (error) {
console.log("error occured");
console.log(error);
}
})

app.get('/hello', async (c) => {
try {
let jsonnet = new Jsonnet();
const code = `
local username = std.extVar('name');
local Person(name='Alice') = {
name: name,
welcome: 'Hello ' + name + '!',
};
{
person1: Person(username),
person2: Person('Bob'),
}`;
let result = jsonnet.extString("name", "ll").evaluateSnippet(code);
return c.json(JSON.parse(result));
} catch (error) {
console.log(error)
}
})

app.fire();

// serve(app, (info) => {
// console.log(`Server started on http://localhost:${info.port}`);
// });
13 changes: 13 additions & 0 deletions JS/wasm/examples/hono-openai/src/prompts/config.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
model: std.extVar("model"),
messages:[
{
role:"system",
content:std.extVar("prompt")
},
{
role:"user",
content:std.extVar("query")
}
]
}
6 changes: 6 additions & 0 deletions JS/wasm/examples/hono-openai/src/prompts/quote.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local prompt =|||
You will provide the user with inspiring, thought-provoking, or funny quotes every time they ask for one. It could be quotes from famous philosophers, authors, celebrities, or even random quotes that will make them smile. Each quote will come with the name of the person who said it and must include a short description of their background and achievements. Ask the user for a brief description of what kind of quote they would like to receive, for example "a funny quote about life". If the description leaves out details, such as what type (inspirational, for example) of quote or the subject (dogs, for example) of the quote you should randomly generate a replacement and find a quote relating to that. If the user is doesn't have a subject in mind or would just like a random quote, generate a quote of a random type and subject and give it to the user. If the user shows disapproval for a quote, make a note of it and immediately provide a quote, relating to the same subjects as before, from a different person. If the user approves of a quote, immediately provide another quote that relates with the original. If the user asks about the person who originally said the quote, provide a brief description of the person's background and achievements that relate to the quote.
|||;
{
"prompt" : prompt,
}

0 comments on commit 3e2b01e

Please sign in to comment.