Skip to content
Snippets Groups Projects
Commit c40c0cab authored by Embruch, Gerd's avatar Embruch, Gerd
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
#MODEL TO USE (get name from list at https://ollama.com/library)
llmName=mistral
\ No newline at end of file
.venv
.env
\ No newline at end of file
# Prerequisits
- python3 installed
- root access to install & configure ollama
# Install
## python env
```
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp ./.env.template ./.env
```
## ollama
### installation
```
sudo curl -fsSL https://ollama.com/install.sh | sh
```
### [OPTIONAL] change model storage folder
By default the models are stored in `/usr/share/ollama/.ollama/models/`
run `sudo systemctl edit ollama.service` to add `OLLAMA_MODELS` as storage path:
```
### Editing /etc/systemd/system/ollama.service.d/override.conf
### Anything between here and the comment below will become the contents of the drop-in file
[Service]
Environment="OLLAMA_MODELS=/local/models/"
### Edits below this comment will be discarded
### /etc/systemd/system/ollama.service
# [Unit]
# Description=Ollama Service
# After=network-online.target
#
# [Service]
# ExecStart=/usr/local/bin/ollama serve
# User=ollama
# Group=ollama
# Restart=always
# RestartSec=3
# Environment="PATH=/usr/local/cuda/bin:/usr/local/bin:/usr/bin:/bin"
#
# [Install]
# WantedBy=default.target
```
run `sudo systemctl deamon-reload && sudo systemctl restart ollama` to reload the config
# Configure
populate the `.env` file with proper information
# Start
```
python3 main.py
```
# Sources
- [LangChain: Run LLMs locally](https://python.langchain.com/docs/guides/development/local_llms/)
- [Ollama](https://ollama.com/)
main.py 0 → 100644
import os
import subprocess
from pathlib import Path
# .env parser
from dotenv import load_dotenv
# colored print
from colorist import Color, BrightColor, bright_yellow, magenta, red, green
# langchain stuff
from langchain_community.llms import Ollama
from langchain.prompts import PromptTemplate
##################################################
### PREPARING & STARTING
##################################################
green('preparing')
##########
# SET VARS
##########
print('loading environment')
# Load environment variables from .env file
if not os.path.isfile('./.env'):
raise RuntimeError("Aborting: No .env file found.")
load_dotenv()
##########
# INSTALLING LLM
##########
print(f"installing llm <{os.environ['llmName']}>. This can take a while, depending on size and if already installed.")
# create download folder
Path(os.environ['OLLAMA_MODELS']).mkdir(parents=True, exist_ok=True)
process = subprocess.run(["ollama","pull" , f"{os.environ['llmName']}"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# on error
if process.returncode != 0:
magenta(process.stderr.decode('utf8'))
red(f"ABORTING: Unable to install the requested LLM")
os._exit(1)
print(process.stdout.decode('UTF8'))
llm = Ollama(
model=os.environ['llmName'],
temperature=0,
# callback_manager=CallbackManager([StreamingStdOutCallbackHandler()])
)
# write down the question to ask
question="What tall is the tallest pyramid in egypt and what's it called?"
##################################################
### ASK THE LLM
##################################################
green('prompting')
# define template
prompt_template = PromptTemplate(
input_variables=['question'],
template = """You are a Teacher / Professor. One of your students asks this question.
Generate the answer based only on the query. Restrict the answer to the topic.
Don't pretend to make a conversation. State the answer clear and precise. The students question is:
---------------------
{question}
---------------------
"""
)
# create prompt by injecting the question
prompt = prompt_template.format(question=question)
# run the query
response = llm.invoke(prompt)
# return the response
print(f"Question: {Color.MAGENTA}{question}{Color.OFF}\nAnswer: {BrightColor.MAGENTA}{response}{Color.OFF}\n")
aiohttp==3.9.5
aiosignal==1.3.1
annotated-types==0.6.0
attrs==23.2.0
certifi==2024.2.2
charset-normalizer==3.3.2
colorist==1.7.2
dataclasses-json==0.6.4
frozenlist==1.4.1
greenlet==3.0.3
idna==3.7
jsonpatch==1.33
jsonpointer==2.4
langchain==0.1.16
langchain-community==0.0.33
langchain-core==0.1.44
langchain-text-splitters==0.0.1
langsmith==0.1.49
marshmallow==3.21.1
multidict==6.0.5
mypy-extensions==1.0.0
numpy==1.26.4
orjson==3.10.1
packaging==23.2
pydantic==2.7.0
pydantic_core==2.18.1
python-dotenv==1.0.1
PyYAML==6.0.1
requests==2.31.0
SQLAlchemy==2.0.29
tenacity==8.2.3
typing-inspect==0.9.0
typing_extensions==4.11.0
urllib3==2.2.1
yarl==1.9.4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment