Skip to content

Commit 9c5f573

Browse files
Merge pull request #58 from vinay235/main
Add Ollama support and docs
2 parents 3d9ddab + 6205e1b commit 9c5f573

File tree

3 files changed

+141
-10
lines changed

3 files changed

+141
-10
lines changed

docs/getting_started.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@ If everything worked, we should now be able to start chatting with our personal
6565

6666
**Setup: Run with Ollama**
6767

68-
How-to-Guide will be written here soon, but it should be fairly simple with [Ollama](https://ollama.ai/) serve and `ngrok http 11434`
69-
70-
```
71-
brew install ngrok/ngrok/ngrok
72-
```
68+
Check under [How to guides](https://docs.adeus.ai/guides/guides.html)
7369

7470
---
7571

docs/guides/ollama_ngrok_setup.md

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: Setup Ollama and ngrok
3+
description: Running Ollama Locally and Serving with Ngrok
4+
layout: default
5+
parent: How to Guides
6+
---
7+
8+
# Setup Ollama and ngrok
9+
{: .no_toc }
10+
11+
## Table of contents
12+
{: .no_toc .text-delta }
13+
14+
1. TOC
15+
{:toc}
16+
17+
---
18+
19+
## Intro
20+
If you would like to setup a local LLM server, you can use the following steps to setup Ollama and serve it with ngrok.
21+
22+
23+
## Setup Ollama locally
24+
25+
There are several ways to setup Ollama, the easiest way is to setup using docker but they support Linux and Mac installs and currently have a Windows preview as well!
26+
Check out [Ollama](https://ollama.com) and on [Github](https://github.com/ollama/ollama) for the complete info.
27+
28+
29+
1. Install the relevant package for Ollama based on your OS
30+
31+
2. Download Mistral model through Ollama CLI
32+
```bash
33+
ollama pull mistral
34+
```
35+
36+
3. Serve it over localhost
37+
```bash
38+
ollama serve
39+
```
40+
41+
You should now be able to see a message saying "Ollama is running" on [http://localhost:11434](http://localhost:11434)
42+
43+
4. Test Mistral model using curl
44+
```bash
45+
curl http://localhost:11434/v1/chat/completions \
46+
-H "Content-Type: application/json" \
47+
-d '{
48+
"model": "mistral",
49+
"messages": [
50+
{
51+
"role": "system",
52+
"content": "You are a helpful assistant."
53+
},
54+
{
55+
"role": "user",
56+
"content": "Hello!"
57+
}
58+
]
59+
}'
60+
```
61+
62+
63+
## Setup ngrok
64+
65+
To provide access to your supabase functions and your chat app, you would need to expose this to the internet, one of the options for the same is [ngrok](https://ngrok.com/).
66+
67+
Let's do a quick setup for ngrok.
68+
69+
70+
1. Download and Install Ngrok. If you haven't already installed ngrok, download it from ngrok's [website](https://ngrok.com/download) and follow the installation instructions for your operating system.
71+
72+
2. Sign-up on ngrok for your authtoken
73+
74+
```bash
75+
ngrok config add-authtoken <token>
76+
```
77+
78+
3. Start an Ngrok Tunnel
79+
Open the exe and start an ngrok tunnel to the port where Ollama is running (usually its 11434)
80+
```bash
81+
ngrok http 11434
82+
```
83+
Ngrok will display a screen with several pieces of information, including the public URL that ngrok provides. It will look something like this:
84+
```bash
85+
Forwarding https://f4dc-2001-bb6.ngrok-free.app -> http://localhost:11434
86+
```
87+
88+
4. Access Ollama from the Internet
89+
Copy the ngrok URL (http://123abc.ngrok.io in the example above) and paste it into your web browser. You should now be able to access your local Ollama instance from anywhere on the internet.
90+
Remember that ngrok sessions are temporary. If you restart ngrok, you'll get a new URL.
91+
92+
5. Secure Your Tunnel (Optional)
93+
If you plan to use ngrok for an extended period or for sensitive applications, consider securing your tunnel with additional options like basic authentication, which can be done as follows:
94+
95+
```bash
96+
ngrok http 11434 --basic-auth="<username>:<password>"
97+
```
98+
You've now successfully set up Ollama to run locally and made it accessible over the internet using ngrok. Remember to monitor your ngrok tunnel and be aware of its usage limits and security implications.
99+
100+
101+
## Use Ollama with Adeus
102+
103+
To use the Ollama and ngrok that you setup with Adeus chat application, setup the public url of ngrok in the supabase secret as OLLAMA_BASE_URL
104+
105+
```bash
106+
supabase secrets set OLLAMA_BASE_URL=https://f4dc-2001-bb6.ngrok-free.app
107+
```
108+
109+
You should now be able to chat with your local model on Adeus!
110+
Ps: The embeddings still use OpenAI's embedding since Ollama doesn't support the OpenAI's embedding endpoints yet. It will be added as part of the code as soon as it is released. So for now you need to setup both OLLAMA_BASE_URL and OPENAI_API_KEY in the supabase secret.

supabase/functions/chat/index.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@ import { ApplicationError, UserError } from "../common/errors.ts";
77

88
async function generateResponse(
99
useOpenRouter,
10+
useOllama,
1011
openaiClient,
1112
openRouterClient,
13+
ollamaClient,
1214
messages
1315
) {
14-
const client = useOpenRouter ? openRouterClient : openaiClient;
15-
const modelName = useOpenRouter
16-
? "nousresearch/nous-capybara-34b"
17-
: "gpt-4-1106-preview";
16+
17+
let client;
18+
let modelName;
19+
20+
if (useOpenRouter) {
21+
client = openRouterClient;
22+
modelName = "nousresearch/nous-capybara-34b";
23+
} else if (useOllama) {
24+
client = ollamaClient;
25+
modelName = "mistral";
26+
} else {
27+
client = openaiClient;
28+
modelName = "gpt-4-1106-preview";
29+
}
1830

1931
const { choices } = await client.chat.completions.create({
2032
model: modelName,
@@ -60,6 +72,17 @@ const chat = async (req) => {
6072
});
6173
}
6274

75+
const ollamaApiKey = Deno.env.get("OLLAMA_BASE_URL");
76+
const useOllama = Boolean(ollamaApiKey); // Use Ollama if OLLAMA_BASE_URL is available
77+
78+
let ollamaClient;
79+
if (useOllama) {
80+
ollamaClient = new OpenAI({
81+
baseURL: Deno.env.get("OLLAMA_BASE_URL"),
82+
apiKey: "ollama"
83+
});
84+
}
85+
6386
console.log("messageHistory: ", messageHistory);
6487

6588
// Embed the last messageHistory message using OpenAI's embeddings API
@@ -88,7 +111,7 @@ const chat = async (req) => {
88111
let messages = [
89112
{
90113
role: "system",
91-
content: `You are a helpful assistant, helping the user navigate through life. He is asking uoi questions, and you answer them with the best of your ability.
114+
content: `You are a helpful assistant, helping the user navigate through life. He is asking you questions, and you answer them with the best of your ability.
92115
You have access to some of their records, to help you answer their question in a more personalized way.
93116
94117
Records:
@@ -102,8 +125,10 @@ const chat = async (req) => {
102125
try {
103126
const responseMessage = await generateResponse(
104127
useOpenRouter,
128+
useOllama,
105129
openaiClient,
106130
openRouterClient,
131+
ollamaClient,
107132
messages
108133
);
109134

0 commit comments

Comments
 (0)