Skip to content

Commit

Permalink
add ollama model server
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelClifford committed Feb 16, 2024
1 parent 4899efe commit 2d56941
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
19 changes: 19 additions & 0 deletions chatbot-langchain/chatbot_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,41 @@
from langchain_community.callbacks import StreamlitCallbackHandler
from langchain_core.prompts import ChatPromptTemplate
import streamlit as st
import requests
import json
import os


model_service = os.getenv("MODEL_SERVICE_ENDPOINT",
"http://localhost:8001/v1")
@st.cache_data
def get_models():
try:
response = requests.get(f"{model_service[:-2]}api/tags")
return [i["name"].split(":")[0] for i in
json.loads(response.content)["models"]]
except:
return None

models = get_models()
model = ""

st.title("💬 Chatbot")
if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "assistant",
"content": "How can I help you?"}]

if models:
with st.sidebar:
model = st.radio(label="Select Model",
options=models)

for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])

llm = ChatOpenAI(base_url=model_service,
api_key="sk-no-key-required",
model=model,
streaming=True,
callbacks=[StreamlitCallbackHandler(st.container(),
expand_new_thoughts=True,
Expand Down
43 changes: 43 additions & 0 deletions model_servers/ollama/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Ollama 🦙 Model Server

We can also use the official [Ollama](https://ollama.ai) image for our model service, [ollama/ollama:latest](https://hub.docker.com/r/ollama/ollama).

### Get a model

Use the ollama cli to pull a model.

```bash
ollama pull mistral
```

### Run the model server

Run the model server and create a volume mount on your local host to where the ollama models are stored.
```bash
podman run -it --rm -v /<LOCAL_PATH>/.ollama/:/root/.ollama:Z -p 11434:11434 ollama
```

### Interact with your model

Once your service is up and running it will expose a REST API that you can use to interact with your model.

```bash
curl -X POST http://localhost:11434/api/generate -d '{
"model": "mistral",
"prompt":"Why is the sky blue?"
}'
```
Please note that the ollama API is slightly different from the llamacpp python API used throughout this repository. Be sure to make any appropriate changes to application code to accommodate these differences.

The full API docs for the ollama model server can be found here: https://github.com/ollama/ollama/blob/main/docs/api.md


However, the ollama model server **does** currently has partial OpenAI API compatibility and should work with any tool that calls `/v1/chat/completions`

```python
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(base_url="http://localhost:11434/v1",
api_key="sk-no-key-required",
model="mistral")
llm.invoke("hello")
```
1 change: 1 addition & 0 deletions model_servers/ollama/builds/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM ollama/ollama

0 comments on commit 2d56941

Please sign in to comment.