Skip to content

Commit 8ab6144

Browse files
committed
πŸŽ‰ Begin a project.
1 parent a2de7f8 commit 8ab6144

File tree

13 files changed

+376
-1
lines changed

13 files changed

+376
-1
lines changed

β€Ž.devcontainer/compose.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
services:
2+
3+
python-workspace:
4+
build:
5+
context: .
6+
dockerfile: python3.Dockerfile
7+
args:
8+
- USER_NAME=${USER}
9+
volumes:
10+
- ../..:/workspaces:cached
11+
command: sleep infinity
12+
13+
ollama-service:
14+
build:
15+
context: .
16+
dockerfile: ollama.Dockerfile

β€Ž.devcontainer/devcontainer.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "GenAI Sandbox",
3+
4+
"dockerComposeFile": "compose.yml",
5+
"service": "python-workspace",
6+
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
7+
"customizations": {
8+
"vscode": {
9+
"extensions": [
10+
"ms-python.python",
11+
"vallyscode.crayons",
12+
"ms-azuretools.vscode-docker"
13+
]
14+
}
15+
},
16+
"features": {
17+
"git": "latest"
18+
},
19+
"remoteEnv": {
20+
"OLLAMA_HOST": "http://ollama-service:11434"
21+
},
22+
"mounts": [
23+
"source=${localEnv:HOME}${localEnv:USERPROFILE}/.gitconfig,target=/home/vscode/.gitconfig,type=bind,consistency=cached",
24+
"source=${localEnv:HOME}${localEnv:USERPROFILE}/.ssh,target=/home/${localEnv:USER}/.ssh,type=bind,consistency=cached"
25+
],
26+
"remoteUser": "${localEnv:USER}",
27+
// Run commands after the container is created.
28+
"postCreateCommand": "git config --global --add safe.directory ${containerWorkspaceFolder} && pip3 install -r requirements.txt"
29+
}

β€Ž.devcontainer/ollama.Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM ollama/ollama:0.4.1
2+
3+
RUN <<EOF
4+
/bin/sh -c "\
5+
/bin/ollama serve & \
6+
sleep 1 && \
7+
ollama pull granite3-moe:1b"
8+
EOF
9+
10+
ENTRYPOINT ["/bin/ollama"]
11+
EXPOSE 11434
12+
CMD ["serve"]

β€Ž.devcontainer/python3.dockerfile

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Use a minimal base image
2+
FROM python:3.10-alpine
3+
4+
LABEL maintainer="@k33g_org"
5+
6+
ARG USER_NAME=${USER_NAME}
7+
8+
# Set environment variables
9+
ENV LANG=en_US.UTF-8
10+
ENV LANGUAGE=en_US.UTF-8
11+
ENV LC_COLLATE=C
12+
ENV LC_CTYPE=en_US.UTF-8
13+
14+
# Install necessary packages
15+
RUN apk update && apk add --no-cache \
16+
bash \
17+
curl \
18+
wget \
19+
git \
20+
sudo \
21+
&& rm -rf /var/cache/apk/*
22+
23+
# Create a new user with sudo privileges
24+
RUN adduser -D -s /bin/bash ${USER_NAME} \
25+
&& echo "${USER_NAME} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
26+
27+
# Switch to user
28+
USER ${USER_NAME}
29+
30+
# Set the working directory
31+
WORKDIR /home/${USER_NAME}
32+
33+
# Create a virtual environment
34+
RUN python3 -m venv /home/${USER_NAME}/.venv
35+
36+
# Add the virtual environment's bin directory to the PATH
37+
ENV PATH="/home/${USER_NAME}/.venv/bin:$PATH"
38+
39+
# Add the activation command to the .bashrc file
40+
RUN echo "source /home/${USER_NAME}/.venv/bin/activate" >> /home/${USER_NAME}/.bashrc
41+
42+
# Verify the Python environment
43+
RUN /bin/bash -c "source /home/${USER_NAME}/.venv/bin/activate && python3 --version"
44+
45+
# Install OhMyBash
46+
RUN bash -c "$(curl -fsSL https://raw.githubusercontent.com/ohmybash/oh-my-bash/master/tools/install.sh)"

β€Ž.vscode/extensions.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"recommendations": [
3+
"pkief.material-icon-theme",
4+
"pkief.material-product-icons",
5+
"aaron-bond.better-comments",
6+
"equinusocio.vsc-material-theme",
7+
"vallyscode.crayons"
8+
]
9+
}

β€Ž.vscode/settings.json

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"workbench.iconTheme": "material-icon-theme",
3+
"workbench.colorTheme": "Gitpod Light",
4+
"editor.fontSize": 16,
5+
"terminal.integrated.fontSize": 16,
6+
"editor.insertSpaces": true,
7+
"editor.tabSize": 4,
8+
"editor.detectIndentation": true,
9+
"files.autoSave": "afterDelay",
10+
"files.autoSaveDelay": 1000,
11+
"editor.fontFamily": "Menlo",
12+
"[markdown]": {
13+
"editor.unicodeHighlight.ambiguousCharacters": false,
14+
"editor.unicodeHighlight.invisibleCharacters": false,
15+
"diffEditor.ignoreTrimWhitespace": false,
16+
"editor.fontWeight": "normal",
17+
"editor.fontFamily": "'Droid Sans Mono', 'monospace', monospace",
18+
"editor.fontSize": 16,
19+
"editor.wordWrap": "on",
20+
"editor.quickSuggestions": {
21+
"comments": "off",
22+
"strings": "off",
23+
"other": "off"
24+
}
25+
},
26+
"[dockerfile]": {
27+
"editor.fontSize": 16,
28+
},
29+
"[dockercompose]": {
30+
"editor.fontSize": 16,
31+
},
32+
"[python]": {
33+
"editor.fontSize": 16,
34+
35+
},
36+
"[json]": {
37+
"editor.fontSize": 16,
38+
39+
},
40+
"[yaml]": {
41+
"editor.fontSize": 16,
42+
43+
},
44+
"python.defaultInterpreterPath": "~/.venv/bin/python"
45+
}

β€Ž01-first-contact/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# First Contact
2+
3+
Start the program by running the following command:
4+
```bash
5+
python3 main.py
6+
```
7+
8+
Then try various questions:
9+
```
10+
Who is James T. Kirk?
11+
Who is Spock?
12+
Who is Jean-Luc Picard?
13+
```
14+
15+

β€Ž01-first-contact/main.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
from ollama import Client
3+
4+
OLLAMA_HOST = os.getenv('OLLAMA_HOST')
5+
ollama_client = Client(host=OLLAMA_HOST)
6+
7+
instructions = """You are an expert of the StarTrek universe.
8+
Make only short answers. Speak like a Vulcan
9+
"""
10+
11+
while True:
12+
user_input = input("πŸ€– (type 'bye' to exit):> ")
13+
if user_input.lower() == "bye":
14+
print("πŸ‘‹ Goodbye!")
15+
break
16+
else:
17+
stream = ollama_client.chat(
18+
model='granite3-moe:1b',
19+
messages=[
20+
{'role': 'system', 'content': instructions},
21+
{'role': 'user', 'content': user_input},
22+
],
23+
options={"temperature":0.5},
24+
stream=True,
25+
)
26+
27+
for chunk in stream:
28+
print(chunk['message']['content'], end='', flush=True)
29+
30+
print("\n")

β€Ž02-more-context/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# More context
2+
3+
Start the program by running the following command:
4+
```bash
5+
python3 main.py
6+
```
7+
8+
Then try various questions:
9+
```
10+
Who is the best friend of James T. Kirk?
11+
What are the enemies of James Kirk?
12+
```

β€Ž02-more-context/main.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import os
2+
from ollama import Client
3+
4+
OLLAMA_HOST = os.getenv('OLLAMA_HOST')
5+
ollama_client = Client(host=OLLAMA_HOST)
6+
7+
instructions = """You are an expert of the StarTrek universe.
8+
Make only short answers. Speak like a Vulcan
9+
"""
10+
11+
context="""
12+
James Tiberius Kirk is a fictional character in the Star Trek media franchise.
13+
Kirk was first played by William Shatner as the captain of the USS Enterprise in the Star Trek: The Original Series.
14+
15+
The best friends of James T. Kirk are:
16+
- Spock: The Vulcan science officer of the USS Enterprise.
17+
- Leonard McCoy: The ship's chief medical officer.
18+
19+
Here are some of main adversaries/enemies of James T. Kirk:
20+
- Klingons: The warrior race was often at odds with the Federation and Kirk personally.
21+
- Khan Noonien Singh: A genetically engineered superhuman and Kirk's most famous nemesis.
22+
- Romulans: Another major alien race often in conflict with the Federation.
23+
"""
24+
25+
while True:
26+
user_input = input("πŸ€– (type 'bye' to exit):> ")
27+
if user_input.lower() == "bye":
28+
print("πŸ‘‹ Goodbye!")
29+
break
30+
else:
31+
stream = ollama_client.chat(
32+
model='granite3-moe:1b',
33+
messages=[
34+
{'role': 'system', 'content': instructions},
35+
{'role': 'system', 'content': context},
36+
{'role': 'user', 'content': user_input},
37+
],
38+
options={"temperature":0.5},
39+
stream=True,
40+
)
41+
42+
for chunk in stream:
43+
print(chunk['message']['content'], end='', flush=True)
44+
45+
print("\n")

β€ŽREADME.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# learn-ollama-api-with-python
1+
# Learn Ollama API with Python
2+
3+
Experiments with [granite3-moe:1b](https://ollama.com/library/granite3-moe:1b)
4+
5+
This is a devcontainer project for learning Ollama API with Python.

β€Žgit.sh

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/bin/bash
2+
message=""
3+
case $1 in
4+
5+
# 🎨: art
6+
art)
7+
message="Improve structure / format of the code"
8+
emoji="🎨"
9+
;;
10+
11+
# πŸ›: bug
12+
bug|fix)
13+
message="Fix a bug"
14+
emoji="πŸ›"
15+
;;
16+
17+
# ✨: sparkles
18+
sparkles|feature)
19+
message="Introduce new feature(s)"
20+
emoji="✨"
21+
;;
22+
23+
# πŸ“: memo
24+
memo|doc|documentation)
25+
message="Add or update documentation"
26+
emoji="πŸ“"
27+
;;
28+
29+
# 🌸: cherry_blossom
30+
gardening|garden|clean|cleaning)
31+
message="Gardening"
32+
emoji="🌸"
33+
;;
34+
35+
# πŸš€: rocket
36+
rocket|deploy)
37+
message="Deploy stuff"
38+
emoji="πŸš€"
39+
;;
40+
41+
# πŸŽ‰: tada
42+
tada|first)
43+
message="Begin a project"
44+
emoji="πŸŽ‰"
45+
;;
46+
47+
# 🚧: construction
48+
construction|wip)
49+
message="Work in progress"
50+
emoji="🚧"
51+
;;
52+
53+
# πŸ“¦οΈ: package
54+
package|build)
55+
message="Add or update compiled files or packages"
56+
emoji="πŸ“¦οΈ"
57+
;;
58+
59+
# πŸ“¦οΈ: package
60+
release)
61+
message="Create a release"
62+
emoji="πŸ“¦οΈ"
63+
;;
64+
65+
# πŸ‘½οΈ: alien
66+
alien|api)
67+
message="Update code due to external API changes"
68+
emoji="πŸ‘½οΈ"
69+
;;
70+
71+
# 🐳: whale
72+
docker|container)
73+
message="Docker"
74+
emoji="🐳"
75+
;;
76+
77+
# 🍊: tangerine
78+
gitpod|gitpodify)
79+
message="Gitpodify"
80+
emoji="🍊"
81+
;;
82+
83+
# πŸ§ͺ: test tube
84+
alembic|experiments|experiment|xp)
85+
message="Perform experiments"
86+
emoji="πŸ§ͺ"
87+
;;
88+
89+
# πŸ’Ύ: floppy-disk
90+
save)
91+
message="Saved"
92+
emoji="πŸ’Ύ"
93+
;;
94+
95+
*)
96+
message="Updated"
97+
emoji="πŸ›Ÿ"
98+
;;
99+
100+
esac
101+
102+
find . -name '.DS_Store' -type f -delete
103+
104+
if [ -z "$2" ]
105+
then
106+
# empty
107+
git add .; git commit -m "$emoji $message."; git push
108+
else
109+
# not empty
110+
git add .; git commit -m "$emoji $message: $2"; git push
111+
fi

β€Žrequirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ollama

0 commit comments

Comments
Β (0)