Skip to content

Commit 4222f8e

Browse files
authored
Merge pull request #8 from pamelafox/newstuff
Add all examples
2 parents efcc2ae + d886272 commit 4222f8e

7 files changed

Lines changed: 453 additions & 3 deletions

File tree

.devcontainer/ollama/devcontainer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@
2525
},
2626

2727
// Use 'postCreateCommand' to run commands after the container is created.
28-
"postCreateCommand": "pip3 install --user -r requirements-dev.txt && cp .env.sample.ollama .env",
28+
"postCreateCommand": "pip3 install --user -r requirements-dev.txt && cp .env.sample.ollama .env && ollama pull llama3.1",
2929

3030
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
31-
"remoteUser": "vscode"
31+
"remoteUser": "vscode",
32+
33+
// Required to handle Ollama models
34+
"hostRequirements": {
35+
"memory": "64gb"
36+
}
3237
}

.devcontainer/openai/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
22
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/python-3
33
{
4-
"name": "python-openai-demos (Ollama)",
4+
"name": "python-openai-demos (OpenAI.com)",
55
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",
66
// Configure tool-specific properties.
77
"customizations": {

few_shot_examples.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os
2+
3+
import azure.identity
4+
import openai
5+
from dotenv import load_dotenv
6+
7+
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
8+
load_dotenv(override=True)
9+
API_HOST = os.getenv("API_HOST")
10+
11+
if API_HOST == "azure":
12+
13+
token_provider = azure.identity.get_bearer_token_provider(
14+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
15+
)
16+
client = openai.AzureOpenAI(
17+
api_version=os.getenv("AZURE_OPENAI_VERSION"),
18+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
19+
azure_ad_token_provider=token_provider,
20+
)
21+
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
22+
23+
elif API_HOST == "ollama":
24+
25+
client = openai.OpenAI(
26+
base_url=os.getenv("OLLAMA_ENDPOINT"),
27+
api_key="nokeyneeded",
28+
)
29+
MODEL_NAME = os.getenv("OLLAMA_MODEL")
30+
31+
elif API_HOST == "github":
32+
33+
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
34+
MODEL_NAME = os.getenv("GITHUB_MODEL")
35+
36+
else:
37+
38+
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
39+
MODEL_NAME = os.getenv("OPENAI_MODEL")
40+
41+
42+
SYSTEM_MESSAGE = """
43+
You are a helpful assistant that helps students with their homework.
44+
Instead of providing the full answer, you respond with a hint or a clue.
45+
"""
46+
47+
48+
USER_MESSAGE = "What is the largest planet in our solar system?"
49+
50+
51+
response = client.chat.completions.create(
52+
model=MODEL_NAME,
53+
temperature=0.7,
54+
n=1,
55+
messages=[
56+
{"role": "system", "content": SYSTEM_MESSAGE},
57+
{"role": "user", "content": "What is the capital of France?"},
58+
{"role": "assistant", "content": "Can you remember the name of the city that is known for the Eiffel Tower?"},
59+
{"role": "user", "content": "What is the square root of 144?"},
60+
{"role": "assistant", "content": "What number multiplied by itself equals 144?"},
61+
{"role": "user", "content": "What is the atomic number of oxygen?"},
62+
{"role": "assistant", "content": "How many protons does an oxygen atom have?"},
63+
{"role": "user", "content": USER_MESSAGE},
64+
],
65+
)
66+
67+
68+
print("Response:")
69+
print(response.choices[0].message.content)

function_calling.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os
2+
3+
import azure.identity
4+
import openai
5+
from dotenv import load_dotenv
6+
7+
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
8+
load_dotenv(override=True)
9+
API_HOST = os.getenv("API_HOST")
10+
11+
if API_HOST == "azure":
12+
13+
token_provider = azure.identity.get_bearer_token_provider(
14+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
15+
)
16+
client = openai.AzureOpenAI(
17+
api_version=os.getenv("AZURE_OPENAI_VERSION"),
18+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
19+
azure_ad_token_provider=token_provider,
20+
)
21+
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
22+
23+
elif API_HOST == "ollama":
24+
25+
client = openai.OpenAI(
26+
base_url=os.getenv("OLLAMA_ENDPOINT"),
27+
api_key="nokeyneeded",
28+
)
29+
MODEL_NAME = os.getenv("OLLAMA_MODEL")
30+
31+
elif API_HOST == "github":
32+
33+
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
34+
MODEL_NAME = os.getenv("GITHUB_MODEL")
35+
36+
else:
37+
38+
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
39+
MODEL_NAME = os.getenv("OPENAI_MODEL")
40+
41+
42+
tools = [
43+
{
44+
"type": "function",
45+
"function": {
46+
"name": "lookup_weather",
47+
"description": "Lookup the weather for a given city name or zip code.",
48+
"parameters": {
49+
"type": "object",
50+
"properties": {
51+
"city_name": {
52+
"type": "string",
53+
"description": "The city name",
54+
},
55+
"zip_code": {
56+
"type": "string",
57+
"description": "The zip code",
58+
},
59+
},
60+
"additionalProperties": False,
61+
},
62+
},
63+
}
64+
]
65+
66+
response = client.chat.completions.create(
67+
model=MODEL_NAME,
68+
messages=[
69+
{"role": "system", "content": "You are a weather chatbot."},
70+
{"role": "user", "content": "Hi, whats the weather like in berkeley?"},
71+
],
72+
tools=tools,
73+
)
74+
75+
print("Response:")
76+
print(response.choices[0].message.tool_calls[0].function.arguments)

hybrid.csv

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
vehicle,year,msrp,acceleration,mpg,class
2+
Prius (1st Gen),1997,24509.74,7.46,41.26,Compact
3+
Tino,2000,35354.97,8.2,54.1,Compact
4+
Prius (2nd Gen),2000,26832.25,7.97,45.23,Compact
5+
Insight,2000,18936.41,9.52,53.0,Two Seater
6+
Civic (1st Gen),2001,25833.38,7.04,47.04,Compact
7+
Insight,2001,19036.71,9.52,53.0,Two Seater
8+
Insight,2002,19137.01,9.71,53.0,Two Seater
9+
Alphard,2003,38084.77,8.33,40.46,Minivan
10+
Insight,2003,19137.01,9.52,53.0,Two Seater
11+
Civic,2003,14071.92,8.62,41.0,Compact
12+
Escape,2004,36676.1,10.32,31.99,SUV
13+
Insight,2004,19237.31,9.35,52.0,Two Seater
14+
Prius,2004,20355.64,9.9,46.0,Midsize
15+
Silverado 15 2WD,2004,30089.64,9.09,17.0,Pickup Truck
16+
Lexus RX400h,2005,58521.14,12.76,28.23,SUV
17+
Civic (2nd Gen),2005,26354.44,7.63,39.99,Compact
18+
Highlander,2005,29186.21,12.76,29.4,SUV
19+
Insight,2005,19387.76,9.71,52.0,Two Seater
20+
Civic,2005,18236.33,8.26,41.0,Compact
21+
Escape 2WD,2005,19322.56,9.52,29.0,SUV
22+
Accord,2005,16343.69,14.93,28.0,Midsize
23+
Silverado 15 2WD,2005,32647.26,11.11,17.0,Pickup Truck
24+
Mercury Mariner,2006,34772.4,8.98,32.93,SUV
25+
Camry,2006,29853.25,11.28,33.64,Midsize
26+
Lexus GS450h,2006,64547.56,18.65,33.4,Midsize
27+
Estima,2006,36012.7,9.26,47.04,Minivan
28+
Altima,2006,29524.75,13.29,32.93,Midsize
29+
Chevrolet Tahoe,2007,42924.35,10.91,22.35,SUV
30+
Kluger,2007,46229.48,12.76,25.87,SUV
31+
Lexus LS600h/hL,2007,118543.6,17.54,21.0,Midsize
32+
Tribute,2007,24823.83,11.28,31.75,SUV
33+
GMC Yukon,2007,57094.81,12.28,21.78,SUV
34+
Aura,2007,22110.87,10.87,27.0,Midsize
35+
Vue,2007,22938.33,10.75,26.0,SUV
36+
Silverado 15 2WD,2007,34653.23,11.49,17.0,Pickup Truck
37+
Crown,2008,62290.38,8.7,37.16,Midsize
38+
Cadillac Escalade,2008,78932.81,9.09,22.35,SUV
39+
F3DM,2008,23744.06,9.52,30.11,Midsize
40+
Altima,2008,18675.63,13.7,34.0,Midsize
41+
A5 BSG,2009,11849.43,7.87,35.28,Midsize
42+
Lexus RX450h,2009,46233.36,13.47,31.99,SUV
43+
ML450 Blue HV,2009,60519.83,12.6,23.99,SUV
44+
Prius (3rd Gen),2009,24641.18,9.6,47.98,Compact
45+
S400 Long,2009,96208.93,13.89,26.34,Large
46+
Mercury Milan,2009,30522.57,11.55,40.69,Midsize
47+
Lexus HS250h,2009,38478.15,11.55,54.1,Compact
48+
Avante/Elantra LPI,2009,21872.71,10.21,41.87,Compact
49+
ActiveHybrid X6,2009,97237.9,17.96,18.82,SUV
50+
SAI,2009,39172.44,11.55,54.1,Midsize
51+
Malibu,2009,24768.79,9.09,29.0,Midsize
52+
Vue,2009,26408.67,13.7,28.0,SUV
53+
Aspen HEV,2009,44903.77,13.51,21.0,SUV
54+
Durango,2009,41033.24,8.33,21.0,SUV
55+
Auris HSD,2010,35787.29,8.85,68.21,Compact
56+
CR-Z,2010,21435.54,9.24,37.0,Two Seater
57+
F3DM PHEV,2010,23124.59,9.24,30.15,Midsize
58+
Touareg,2010,64198.95,15.38,28.7,SUV
59+
Audi Q5,2010,37510.86,14.08,33.64,SUV
60+
Jeep Patriot,2010,17045.06,12.05,29.4,SUV
61+
Besturn B50 ,2010,14586.61,7.14,31.28,Midsize
62+
ActiveHybrid 7,2010,104300.43,20.41,22.11,Large
63+
Lincoln MKZ,2010,37036.64,11.15,37.63,Midsize
64+
Fit/Jazz,2010,16911.85,8.26,30.0,Compact
65+
Sonata,2010,28287.66,14.7,37.0,Midsize
66+
Cayenne S,2010,73183.47,14.71,26.11,SUV
67+
Insight,2010,19859.16,9.17,41.0,Compact
68+
Fuga Infiniti M35H,2010,70157.02,18.65,33.64,Midsize
69+
Chevrolet Volt,2010,42924.35,10.78,35.0,Compact
70+
Tribute 4WD,2010,27968.32,12.35,29.0,SUV
71+
Fusion FWD,2010,28033.51,11.49,39.0,Midsize
72+
HS 250h,2010,34753.53,11.76,35.0,Compact
73+
Mariner FWD,2010,30194.95,11.63,32.0,SUV
74+
RX 450h,2010,42812.54,13.89,30.0,SUV
75+
ML450 4natic,2010,55164.33,12.99,22.0,SUV
76+
Silverado 15 2WD,2010,38454.56,11.76,22.0,Pickup Truck
77+
S400,2010,88212.78,12.99,21.0,Large
78+
Aqua,2011,22850.87,9.35,50.0,Compact
79+
Lexus CT200h,2011,30082.16,9.71,42.0,Compact
80+
Civic (3rd Gen),2011,24999.59,9.6,44.36,Compact
81+
Prius alpha (V),2011,30588.35,10.0,72.92,Midsize
82+
3008,2011,45101.54,11.36,61.16,Compact
83+
Fit Shuttle,2011,16394.36,7.52,58.8,Minivan
84+
Buick Regal,2011,27948.93,12.05,25.99,Midsize
85+
Prius V,2011,27272.28,9.51,32.93,Midsize
86+
Freed/Freed Spike,2011,27972.07,6.29,50.81,Minivan
87+
Optima K5,2011,26549.16,10.54,36.0,Midsize
88+
Escape FWD,2011,30661.34,12.35,32.0,SUV
89+
Insight,2011,18254.38,9.52,41.0,Compact
90+
MKZ FWD,2011,34748.52,11.49,39.0,Midsize
91+
CR-Z,2011,19402.8,12.2,37.0,Two Seater
92+
Sonata,2011,25872.07,11.9,36.0,Midsize
93+
Camry,2011,27130.82,13.89,33.0,Midsize
94+
Tribute 2WD,2011,26213.09,12.5,32.0,SUV
95+
Cayenne S,2011,67902.28,18.52,21.0,SUV
96+
Touareg,2011,50149.39,16.13,21.0,SUV
97+
ActiveHybrid 7i,2011,102605.66,18.18,20.0,Midsize
98+
Prius C,2012,19006.62,9.35,50.0,Compact
99+
Prius PHV,2012,32095.61,8.82,50.0,Midsize
100+
Ampera,2012,31739.55,11.11,37.0,Compact
101+
ActiveHybrid 5,2012,62180.23,16.67,26.0,Midsize
102+
Lexus GS450h,2012,59126.14,16.95,31.0,Midsize
103+
Insight,2012,18555.28,9.42,42.0,Compact
104+
Chevrolet Volt,2012,39261.96,11.11,37.0,Compact
105+
Camry LE,2012,26067.66,13.16,41.0,Midsize
106+
MKZ FWD,2012,34858.84,11.49,39.0,Midsize
107+
M35h,2012,53860.45,19.23,29.0,Midsize
108+
LaCrosse,2012,30049.52,11.36,29.0,Midsize
109+
ActiveHybrid 5,2012,61132.11,17.54,26.0,Midsize
110+
Panamera S,2012,95283.85,17.54,25.0,Large
111+
Yukon 1500,2012,52626.77,13.5,21.0,SUV
112+
Prius C,2013,19080.0,8.7,50.0,Compact
113+
Jetta,2013,24995.0,12.66,45.0,Compact
114+
Civic,2013,24360.0,10.2,44.0,Compact
115+
Prius,2013,24200.0,10.2,50.0,Midsize
116+
Fusion FWD,2013,27200.0,11.72,47.0,Midsize
117+
C-Max FWD,2013,25200.0,12.35,43.0,Large
118+
Insight,2013,18600.0,11.76,42.0,Compact
119+
Camry LE,2013,26140.0,13.51,41.0,Midsize
120+
Camry LXLE,2013,27670.0,13.33,40.0,Midsize
121+
Sonata,2013,25650.0,11.76,38.0,Midsize
122+
Optima,2013,25900.0,11.63,38.0,Midsize
123+
Sonata Limited,2013,30550.0,11.76,37.0,Midsize
124+
Optima EX,2013,31950.0,11.36,37.0,Midsize
125+
Malibu,2013,24985.0,11.49,29.0,Midsize
126+
LaCrosse,2013,31660.0,11.36,29.0,Midsize
127+
Regal,2013,29015.0,12.2,29.0,Midsize
128+
RX 450h,2013,46310.0,12.99,30.0,SUV
129+
Highlander 4WD,2013,40170.0,13.89,28.0,SUV
130+
Q5,2013,50900.0,14.71,26.0,SUV
131+
Cayenne S,2013,69850.0,16.39,21.0,SUV
132+
Touareg,2013,62575.0,16.13,21.0,SUV
133+
Escalade 2WD,2013,74425.0,11.63,21.0,SUV
134+
Tahoe 2WD,2013,53620.0,11.9,21.0,SUV
135+
Yukon 1500,2013,54145.0,11.88,21.0,SUV
136+
Yukon 1500,2013,61960.0,13.33,21.0,SUV
137+
MKZ FWD,2013,35925.0,14.03,45.0,Midsize
138+
CT 200h,2013,32050.0,10.31,42.0,Compact
139+
ES 300h,2013,39250.0,12.35,40.0,Midsize
140+
ILX,2013,28900.0,9.26,38.0,Compact
141+
ActiveHybrid 3,2013,49650.0,14.93,28.0,Compact
142+
Silverado 15 2WD,2013,41135.0,12.35,21.0,Pickup Truck
143+
Sierra 15 2WD,2013,41555.0,10.0,21.0,Pickup Truck
144+
GS 450h,2013,59450.0,16.67,31.0,Midsize
145+
M35h,2013,54750.0,19.61,29.0,Midsize
146+
E400,2013,55800.0,14.93,26.0,Midsize
147+
ActiveHybrid 5,2013,61400.0,12.99,26.0,Midsize
148+
ActiveHybrid 7L,2013,84300.0,18.18,25.0,Large
149+
Panamera S,2013,96150.0,18.52,25.0,Large
150+
S400,2013,92350.0,13.89,21.0,Large
151+
Prius Plug-in,2013,32000.0,9.17,50.0,Midsize
152+
C-Max Energi Plug-in,2013,32950.0,11.76,43.0,Midsize
153+
Fusion Energi Plug-in,2013,38700.0,11.76,43.0,Midsize
154+
Chevrolet Volt,2013,39145.0,11.11,37.0,Compact

prompt_engineering.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import os
2+
3+
import azure.identity
4+
import openai
5+
from dotenv import load_dotenv
6+
7+
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
8+
load_dotenv(override=True)
9+
API_HOST = os.getenv("API_HOST")
10+
11+
if API_HOST == "azure":
12+
13+
token_provider = azure.identity.get_bearer_token_provider(
14+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
15+
)
16+
client = openai.AzureOpenAI(
17+
api_version=os.getenv("AZURE_OPENAI_VERSION"),
18+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
19+
azure_ad_token_provider=token_provider,
20+
)
21+
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
22+
23+
elif API_HOST == "ollama":
24+
25+
client = openai.OpenAI(
26+
base_url=os.getenv("OLLAMA_ENDPOINT"),
27+
api_key="nokeyneeded",
28+
)
29+
MODEL_NAME = os.getenv("OLLAMA_MODEL")
30+
31+
elif API_HOST == "github":
32+
33+
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
34+
MODEL_NAME = os.getenv("GITHUB_MODEL")
35+
36+
else:
37+
38+
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
39+
MODEL_NAME = os.getenv("OPENAI_MODEL")
40+
41+
42+
SYSTEM_MESSAGE = """
43+
I want you to act like Elmo from Sesame Street.
44+
I want you to respond and answer like Elmo using the tone, manner and vocabulary that Elmo would use.
45+
Do not write any explanations. Only answer like Elmo.
46+
You must know all of the knowledge of Elmo, and nothing more.
47+
"""
48+
49+
USER_MESSAGE = """
50+
Hi Elmo, how are you doing today?
51+
"""
52+
53+
response = client.chat.completions.create(
54+
model=MODEL_NAME,
55+
temperature=0.7,
56+
n=1,
57+
messages=[
58+
{"role": "system", "content": SYSTEM_MESSAGE},
59+
{"role": "user", "content": USER_MESSAGE},
60+
],
61+
)
62+
63+
print("Response:")
64+
print(response.choices[0].message.content)

0 commit comments

Comments
 (0)