Skip to content

Commit

Permalink
AI proxy spin app (#23)
Browse files Browse the repository at this point in the history
- copied code from petersalomonsen/near-openai#11, which adds a [spin](https://github.com/fermyon/spin) app with a proxy to the OpenAI API, using the Fungible Token contract to cover usage cost
- hash `conversation_id` when calling the smart contract from the proxy to ensure that the smart contract does not reveal the conversation id that the AI proxy accepts
- added a test for the [aiconversation.js](https://github.com/petersalomonsen/quickjs-rust-near/blob/master/examples/fungibletoken/e2e/aiconversation.js) script that also shows the full process of starting a conversation and to get the refund when done
  • Loading branch information
petersalomonsen authored Dec 14, 2024
1 parent 5094835 commit 4a38d5e
Show file tree
Hide file tree
Showing 16 changed files with 2,911 additions and 5 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/test-aiproxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "AI proxy tests"

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:
name: "AI proxy"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: |
curl -fsSL https://developer.fermyon.com/downloads/install.sh | bash
sudo mv spin /usr/local/bin/
rustup target add wasm32-wasi
cargo install cargo-component --locked
spin plugin install -y -u https://github.com/fermyon/spin-test/releases/download/canary/spin-test.json
- name: Check formatting
working-directory: examples/aiproxy
run: |
(cd openai-proxy && cargo fmt --check)
(cd tests && cargo fmt --check)
- name: Build project
working-directory: examples/aiproxy
run: |
spin build
- name: Run tests
working-directory: examples/aiproxy
run: |
spin test
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"rust-analyzer.linkedProjects": [
"examples/nft/Cargo.toml",
"examples/minimumweb4/Cargo.toml",
"examples/purejs/Cargo.toml"
"examples/purejs/Cargo.toml",
"examples/aiproxy/openai-proxy/Cargo.toml",
"examples/aiproxy/tests/Cargo.toml"
]

}
1 change: 1 addition & 0 deletions examples/aiproxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
26 changes: 26 additions & 0 deletions examples/aiproxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# AI Proxy

This folder contains a [Spin](https://www.fermyon.com/spin) application, based on the WASI 2 and the WebAssembly Component Model ( https://component-model.bytecodealliance.org/ ). It is implemented in Rust as a serverless proxy for the OpenAI API.

There is a simple example of a web client in the [web](./web/) folder.

The application will keep track of of token usage per conversation in the built-in key-value storage of Spin. The initial balance for a conversation is retrieved from the Fungible Token smart contract.

To launch the application, make sure to have the Spin SDK installed. Set the environment variable `SPIN_VARIABLE_OPENAI_API_KEY` to your OpenAI API key.

Then run the following commands:

```
spin build
spin up
```

This will start the OpenAI proxy server at http://localhost:3000

You can also launch the web client using for example [http-server](https://www.npmjs.com/package/http-server):

```
http-server web
```

You will then find the web client at http://localhost:8080. Here you can have a conversation with the AI model.
Loading

0 comments on commit 4a38d5e

Please sign in to comment.