Build a cross-platform ChatGPT application using Rust + Tauri in two days
Published: 2023-05-08 08:04:48

On the rare leisurely May Day holiday, with an itchy hand, I spent two days building a cross-platform ChatGPT application using Rust + Tauri.

I noticed that there are four open-source ChatGPT applications on GitHub with relatively high popularity, including a wrapped desktop version, a naive-ui web version, a nextjs version, and a pure HTML version without server dependencies. So I wanted to write a cross-platform version. The reason for choosing Rust as the backend instead of directly calling the client API is to expect greater scalability in the future and embed my own models.

The [project](https://github.com/Poordeveloper/chatgpt-app/) is now online and included in awesome tauri. The front-end reuses web-chatgpt, while the backend is rewritten in Rust. It can currently run smoothly on desktops, the web, and mobile phones.

The official version of Tauri does not yet support mobile devices, so we use the latest version 2.0.0-alpha.8, and the documentation is available here: [Guides | Tauri Apps](https://next--tauri.netlify.app/next/guides/). Tauri does not currently support using proxies in browsers, but this can be solved using native Rust.

## Prerequisite

- [nodejs](https://nodejs.org/)
- [pnpm](https://pnpm.io/)
- [rust](https://rustup.rs/)
- [Android / iOS sdk](https://next--tauri.netlify.app/next/guides/getting-started/prerequisites/)

## Directory structure
- [src](https://github.com/Poordeveloper/chatgpt-app/tree/main/src): the front-end part, modified from web-chatgpt
- [server](https://github.com/Poordeveloper/chatgpt-app/tree/main/server): the web backend, using axum. Here, in order to write [stream](https://github.com/Poordeveloper/chatgpt-app/blob/9784e0c61e326ca79f65dddccc4e6dbe7a593563/server/src/service.rs#L169), a lot of time was wasted mainly because there were few examples online, and also because it was the first time encountering the concept of stream and there were some misunderstandings.
- [shared](https://github.com/Poordeveloper/chatgpt-app/tree/main/shared): Rust backend logic part, currently only implements the OpenAI API call. It has to be said that Rust's ecosystem is still slightly inferior to nodejs. The functionality of the OpenAI API SDK is slightly inferior. Here, combined with the async-openai crate and [nodejs chatgpt-api](https://github.com/transitive-bullshit/chatgpt-api), [the context function](https://github.com/Poordeveloper/chatgpt-app/blob/9784e0c61e326ca79f65dddccc4e6dbe7a593563/shared/src/gpt.rs#L241) is implemented, and our own model will be added here in the future.
- [src-tauri](https://github.com/Poordeveloper/chatgpt-app/tree/main/src-tauri) : Tauri implementation, which is relatively simple because the main logic is in [shared](https://github.com/Poordeveloper/chatgpt-app/tree/main/shared), which is shared with [server](https://github.com/Poordeveloper/chatgpt-app/tree/main/server).

## Run desktop

`pnpm tauri dev`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuj1xqlrjmd98pqt92gp.png)

## Run web

```
# run backend
cargo run --bin server
# start frontend dev
pnpm dev
```


## Run iOS
`pnpm tauri ios dev`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vou31swyc5rfj1y32996.png)