Skip to content

Commit 3940ad4

Browse files
committed
Dev container
1 parent 08cd8ee commit 3940ad4

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

.devcontainer/devcontainer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2+
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/python-3
3+
{
4+
"name": "Phi-3 Cookbook",
5+
"image": "mcr.microsoft.com/devcontainers/python:3.12-bullseye",
6+
"features": {
7+
"ghcr.io/prulloac/devcontainer-features/ollama:1": {}
8+
},
9+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
10+
"remoteUser": "vscode"
11+
}

code/04.Finetuning/Phi_3_Inference_Finetuning.ipynb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
},
6060
"outputs": [],
6161
"source": [
62-
"This command is run in a bash shell due to '%%bash' at the beginning.\n",
62+
"# This command is run in a bash shell due to '%%bash' at the beginning.\n",
6363
"# 'pip install -qqq' is used to install Python packages with pip, Python's package installer, in a less verbose mode.\n",
6464
"# 'accelerate', 'transformers', 'auto-gptq', and 'optimum' are the packages being installed.\n",
6565
"# These packages are necessary for the fine-tuning and inference of the Phi-3 model.\n",
@@ -140,7 +140,8 @@
140140
"outputs = model.generate(**inputs,\n",
141141
" do_sample=True, max_new_tokens=120)\n",
142142
"\n",
143-
"# Decode the generated tokens and remove any special tokens"
143+
"# Decode the generated tokens and remove any special tokens\n",
144+
"response = tokenizer.decode(outputs[0], skip_special_tokens=True)"
144145
]
145146
},
146147
{

md/02.QuickStart/Ollama_QuickStart.md

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## **1. Installation**
66

7-
Ollama supports running on Windows, macOS, and Linux. You can install Ollama through this link ([https://ollama.com/download](https://ollama.com/download)). After successful installation, you can directly use Ollama script to call Phi-3 through a terminal window. You can see all the [available libaries in Ollama.](https://ollama.com/library)
7+
Ollama supports running on Windows, macOS, and Linux. You can install Ollama through this link ([https://ollama.com/download](https://ollama.com/download)). After successful installation, you can directly use Ollama script to call Phi-3 through a terminal window. You can see all the [available libaries in Ollama](https://ollama.com/library). If you open this repository in a Codespace, it will already have Ollama installed.
88

99

1010
```bash
@@ -26,7 +26,7 @@ If you want to call the Phi-3 API generated by ollama, you can use this command
2626
ollama serve
2727

2828
```
29-
***Note:*** If running MacOS or Linux, please note that you may encounter the following error <b>"Error: listen tcp 127.0.0.1:11434: bind: address already in use"</b> You may get this error when calling running the command. The solution for this problems is:
29+
***Note:*** If running MacOS or Linux, please note that you may encounter the following error <b>"Error: listen tcp 127.0.0.1:11434: bind: address already in use"</b> You may get this error when calling running the command. You can either ignore that error, since it typically indicates the server is already running, or you can stop the and restart Ollama:
3030

3131
**macOS**
3232

@@ -46,7 +46,7 @@ sudo systemctl stop ollama
4646

4747
```
4848

49-
Ollama supports two API: generate and chat. You can call the model API provided by Ollama according to your needs. Local service port 11434. such as
49+
Ollama supports two API: generate and chat. You can call the model API provided by Ollama according to your needs, by sending requests to the local service running on port 11434.
5050

5151
**Chat**
5252

@@ -74,7 +74,7 @@ curl http://127.0.0.1:11434/api/chat -d '{
7474
This is the result in Postman
7575

7676

77-
![chat](../../imgs/02/Ollama/ollama_chat.png)
77+
![Screenshot of JSON results for chat request](../../imgs/02/Ollama/ollama_chat.png)
7878

7979

8080
```bash
@@ -92,11 +92,11 @@ curl http://127.0.0.1:11434/api/generate -d '{
9292
This is the result in Postman
9393

9494

95-
![gen](../../imgs/02/Ollama/ollama_gen.png)
95+
![Screenshot of JSON results for generate request](../../imgs/02/Ollama/ollama_gen.png)
9696

9797
# Additional Resources
9898

99-
Check the list of available models in Ollama in [this link.](https://ollama.com/library)
99+
Check the list of available models in Ollama in [their library](https://ollama.com/library).
100100

101101
Pull your model from the Ollama server using this command
102102

@@ -113,17 +113,45 @@ ollama run phi3
113113
***Note:*** Visit this link [https://github.com/ollama/ollama/blob/main/docs/api.md](https://github.com/ollama/ollama/blob/main/docs/api.md) to learn more
114114

115115

116+
## Calling Ollama from Python
117+
118+
You can use `requests` or `urllib3` to make requests to the local server endpoints used above. However, a popular way to use Ollama in Python is via the [openai](https://pypi.org/project/openai/) SDK, since Ollama provides OpenAI-compatible server endpoints as well.
119+
120+
Here is an example for phi3-mini:
121+
122+
```python
123+
import openai
124+
125+
client = openai.OpenAI(
126+
base_url="http://localhost:11434/v1",
127+
api_key="nokeyneeded",
128+
)
129+
130+
response = client.chat.completions.create(
131+
model="phi3"
132+
temperature=0.7,
133+
n=1,
134+
messages=[
135+
{"role": "system", "content": "You are a helpful assistant."},
136+
{"role": "user", "content": "Write a haiku about a hungry cat"},
137+
],
138+
)
139+
140+
print("Response:")
141+
print(response.choices[0].message.content)
142+
```
143+
116144
## Calling Ollama from JavaScript
117145

118146
```javascript
119-
#Example of Summarize a file with Phi-3
147+
// Example of Summarize a file with Phi-3
120148
script({
121149
model: "ollama:phi3",
122150
title: "Summarize with Phi-3",
123151
system: ["system"],
124152
})
125153

126-
#Example of summarize
154+
// Example of summarize
127155
const file = def("FILE", env.files)
128156
$`Summarize ${file} in a single paragraph.`
129157
```
@@ -160,14 +188,3 @@ Run the app with the command:
160188
```bash
161189
dotnet run
162190
```
163-
164-
165-
166-
167-
168-
169-
170-
171-
172-
173-

0 commit comments

Comments
 (0)