Skip to content

Commit 4962023

Browse files
authored
docs: add ollama tut (#489)
* docs: add ollama tut * fix: changed vector name
1 parent 275959b commit 4962023

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
title: 'Deploying llama3 to Bytebase SQL editor for text2SQL'
3+
author: Dec
4+
tags: Tutorial
5+
updated_at: 2024/12/27 18:15
6+
integrations: General
7+
level: Intermediate
8+
estimated_time: '20 mins'
9+
description: 'In this tutorial, we will demonstrate how to use One API to help deploy llama3 to Bytebase SQL editor'
10+
---
11+
12+
With Open AI-compatible API, it is possible to query database using natural language in Bytebase SQL editor. For data security reasons, privately deploying a large language model is a good option - here we chose the powerful open source model llama3. Since OpenAI blocks outbound traffic by default, to simplify network configuration and further prevent token leakage, we used the open source project One API as a relay to convert between Bytebase's OpenAI API-compliant requests and llama3 API requests.
13+
14+
## Prerequisites
15+
16+
Before you begin, make sure you have:
17+
18+
- [Docker](https://www.docker.com/) installed
19+
- [Bytebase](https://www.bytebase.com/docs/get-started/step-by-step/deploy-with-docker/) instance running
20+
21+
## Get llama3 running in Docker
22+
23+
Run the following command in terminal to get a Docker container running:
24+
25+
```bash
26+
docker run -d -p 11434:11434 --name ollama --restart always ollama/ollama
27+
```
28+
29+
Container starts and returns id, then enter the container with the following command:
30+
31+
```bash
32+
docker exec -it ollama bash
33+
```
34+
35+
Pull and run the llama3 model. Due to permission issues, the model needs to be renamed to gpt-3.5-turbo (or mapped in One-API). After renaming, the model name is gpt-3.5-turbo, but indeed it is still llama3.
36+
37+
```bash
38+
ollama pull llama3
39+
ollama cp llama3 gpt-3.5-turbo
40+
ollama run gpt-3.5-turbo
41+
```
42+
43+
Now that the model is running, you can test if the API is working properly in a new terminal page:
44+
45+
```bash
46+
curl http://localhost:11434/api/generate -d '{
47+
"model": "gpt-3.5-turbo",
48+
"prompt":"Why is the sky blue?"
49+
}'
50+
```
51+
52+
Seeing the results streaming out means that API is working well.
53+
54+
## Configure One API
55+
56+
Choose a directory with read&write permissions (replace `YOUR_PATH` in the following command) to save data and logs. For example, you can use the `pwd` command in the mac terminal to view the current path and replace `YOUR_PATH` with it.
57+
58+
```bash
59+
docker run --name one-api -d --restart always -p 3000:3000 -e TZ=Asia/Shanghai -v YOUR_PATH/one-api:/data justsong/one-api-en
60+
```
61+
62+
Seeing the Docker container start and id output means successful deployment. If you encounter any issues, refer to the solution in One API documentation.
63+
64+
![one-api-docker-run](/content/docs/tutorials/one-api-ollama-sql-editor/one-api-docker-run.webp)
65+
66+
In Docker dashboard, you can see one-api container and its address as well. You can access `localhost:3000` here to log in to One API dashboard.
67+
68+
<HintBlock type="info">
69+
70+
The initial account username is `root`, password is `123456`.
71+
72+
</HintBlock>
73+
74+
### Configure Channel
75+
76+
Enter **channel page**, select **Add a new channel**. Fill in model information:
77+
78+
- **Type**: `ollama`
79+
- **Name**: `llama3`
80+
- **Group**: `default`
81+
- **Model**: `gpt-3.5-turbo`
82+
- **Key**: Anything (for example `SSSS|sssss|1111`) with format `APPID|APISecret|APIKey` if ollama has not set up for key
83+
- **Proxy**: the IP address of the ollama container `http://host.docker.internal:11434`
84+
85+
Furthermore, we mentioned above that the model name can be mapped in One-API. This can be done in the **Model redirection** bar on this page using a JSON string.
86+
87+
### Configure API Keys
88+
89+
In the **API keys** page, click **Add New Token**, and fill in the **Name** (for example `llama3`) and **Model scope** (for example `gpt-3.5-turbo`).
90+
91+
After clicking **Submit**, you will see the new API key in **My keys** list within **API keys** page. Click **Copy** to get a token starting with `sk-`, with witch you can repalce `YOUR_TOKEN` in the code below. If the code runs successfully in your terminal, it means that One API configuration is complete.
92+
93+
```bash
94+
curl http://localhost:3000/v1/chat/completions \
95+
-H "Content-Type: application/json" \
96+
-H "Authorization: Bearer YOUR_TOKEN" \
97+
-d '{
98+
"model": "gpt-3.5-turbo",
99+
"messages": [
100+
{
101+
"role": "user",
102+
"content": "Tell me a joke."
103+
}
104+
],
105+
"temperature": 0.7
106+
}'
107+
```
108+
109+
## Configure Bytebase and run
110+
111+
In Bytebase Workspace, go to **Settings** -> **General**, and scroll down to **AI Assistant** section. Fill `YOUR_TOKEN` we generated in One API into `OpenAI API Key` bar, and fill the `OpenAI API Endpoint` bar with `http://localhost:3000`. Click **Update**.
112+
113+
![bytebase-ai-assistant-config](/content/docs/tutorials/one-api-ollama-sql-editor/bytebase-ai-assistant-config.webp)
114+
115+
Enter **SQL Editor** from top of any page. You can see an OpenAI icon on top right corner. Click it to start conversation with AI assistant, ask questions in natural language and get SQL results.
116+
117+
![bytebase-ai-assistant-sql-editor](/content/docs/tutorials/one-api-ollama-sql-editor/bytebase-ai-assistant-sql-editor.webp)
76.3 KB
Loading
116 KB
Loading
15.7 KB
Loading

0 commit comments

Comments
 (0)