Skip to content

Commit 390eb7c

Browse files
committed
Add support for everything
1 parent 29ea501 commit 390eb7c

19 files changed

+181
-17
lines changed

.devcontainer/devcontainer.json

+2-8
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 3",
4+
"name": "python-openai-demos (Azure OpenAI)",
55
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",
66

77
"features": {
@@ -14,10 +14,7 @@
1414
"vscode": {
1515
// Set *default* container specific settings.json values on container create.
1616
"settings": {
17-
"python.defaultInterpreterPath": "/usr/local/bin/python",
18-
"files.exclude": {
19-
"__pycache__": true
20-
}
17+
"python.defaultInterpreterPath": "/usr/local/bin/python"
2118
},
2219

2320
// Add the IDs of extensions you want installed when the container is created.
@@ -29,9 +26,6 @@
2926
}
3027
},
3128

32-
// Use 'forwardPorts' to make a list of ports inside the container available locally.
33-
// "forwardPorts": [],
34-
3529
// Use 'postCreateCommand' to run commands after the container is created.
3630
"postCreateCommand": "pip3 install --user -r requirements-dev.txt",
3731

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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": "python-openai-demos (GitHub models)",
5+
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",
6+
"features": {
7+
},
8+
// Configure tool-specific properties.
9+
"customizations": {
10+
// Configure properties specific to VS Code.
11+
"vscode": {
12+
// Set *default* container specific settings.json values on container create.
13+
"settings": {
14+
"python.defaultInterpreterPath": "/usr/local/bin/python"
15+
},
16+
17+
// Add the IDs of extensions you want installed when the container is created.
18+
"extensions": [
19+
"ms-python.python",
20+
"charliermarsh.ruff",
21+
"ms-python.black-formatter"
22+
]
23+
}
24+
},
25+
26+
// Use 'postCreateCommand' to run commands after the container is created.
27+
"postCreateCommand": "pip3 install --user -r requirements-dev.txt && cp .env.sample.github .env",
28+
29+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
30+
"remoteUser": "vscode"
31+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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": "python-openai-demos (Ollama)",
5+
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",
6+
"features": {
7+
"ghcr.io/prulloac/devcontainer-features/ollama:1": {}
8+
},
9+
// Configure tool-specific properties.
10+
"customizations": {
11+
// Configure properties specific to VS Code.
12+
"vscode": {
13+
// Set *default* container specific settings.json values on container create.
14+
"settings": {
15+
"python.defaultInterpreterPath": "/usr/local/bin/python"
16+
},
17+
18+
// Add the IDs of extensions you want installed when the container is created.
19+
"extensions": [
20+
"ms-python.python",
21+
"charliermarsh.ruff",
22+
"ms-python.black-formatter"
23+
]
24+
}
25+
},
26+
27+
// Use 'postCreateCommand' to run commands after the container is created.
28+
"postCreateCommand": "pip3 install --user -r requirements-dev.txt && cp .env.sample.ollama .env",
29+
30+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
31+
"remoteUser": "vscode"
32+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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": "python-openai-demos (Ollama)",
5+
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",
6+
// Configure tool-specific properties.
7+
"customizations": {
8+
// Configure properties specific to VS Code.
9+
"vscode": {
10+
// Set *default* container specific settings.json values on container create.
11+
"settings": {
12+
"python.defaultInterpreterPath": "/usr/local/bin/python"
13+
},
14+
15+
// Add the IDs of extensions you want installed when the container is created.
16+
"extensions": [
17+
"ms-python.python",
18+
"charliermarsh.ruff",
19+
"ms-python.black-formatter"
20+
]
21+
}
22+
},
23+
24+
// Use 'postCreateCommand' to run commands after the container is created.
25+
"postCreateCommand": "pip3 install --user -r requirements-dev.txt && cp .env.sample.openai .env",
26+
27+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
28+
"remoteUser": "vscode"
29+
}

.env.sample

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
# API_HOST can be either azure, ollama, or openai:
1+
# API_HOST can be either azure, ollama, openai, or github:
22
API_HOST=azure
33
# Needed for Azure:
44
AZURE_OPENAI_ENDPOINT=https://YOUR-AZURE-OPENAI-SERVICE-NAME.openai.azure.com
55
AZURE_OPENAI_DEPLOYMENT=YOUR-AZURE-DEPLOYMENT-NAME
66
AZURE_OPENAI_VERSION=2024-03-01-preview
77
# Needed for Ollama:
88
OLLAMA_ENDPOINT=http://localhost:11434/v1
9-
OLLAMA_MODEL=llama2
9+
OLLAMA_MODEL=llama3.1
1010
# Needed for OpenAI.com:
1111
OPENAI_KEY=YOUR-OPENAI-KEY
1212
OPENAI_MODEL=gpt-3.5-turbo
13+
# Needed for GitHub models:
14+
GITHUB_MODELS_ENDPOINT="https://models.inference.ai.azure.com"
15+
GITHUB_MODELS_MODEL=gpt-4o

.env.sample.azure

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# See .env.sample for all options
2+
API_HOST=azure
3+
AZURE_OPENAI_ENDPOINT=https://YOUR-AZURE-OPENAI-SERVICE-NAME.openai.azure.com
4+
AZURE_OPENAI_DEPLOYMENT=YOUR-AZURE-DEPLOYMENT-NAME
5+
AZURE_OPENAI_VERSION=2024-03-01-preview

.env.sample.github

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# See .env.sample for all options
2+
API_HOST=github
3+
GITHUB_MODEL=gpt-4o

.env.sample.ollama

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# See .env.sample for all options
2+
API_HOST=ollama
3+
OLLAMA_ENDPOINT=http://localhost:11434/v1
4+
OLLAMA_MODEL=llama3.1

.env.sample.openai

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# See .env.sample for all options
2+
API_HOST=openai
3+
OPENAI_KEY=YOUR-OPENAI-KEY
4+
OPENAI_MODEL=gpt-3.5-turbo

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.exclude": {
3+
"__pycache__": true
4+
}
5+
}

chat.py

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
API_HOST = os.getenv("API_HOST")
1010

1111
if API_HOST == "azure":
12+
1213
token_provider = azure.identity.get_bearer_token_provider(
1314
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
1415
)
@@ -18,13 +19,25 @@
1819
azure_ad_token_provider=token_provider,
1920
)
2021
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
22+
2123
elif API_HOST == "ollama":
24+
2225
client = openai.OpenAI(
2326
base_url=os.getenv("OLLAMA_ENDPOINT"),
2427
api_key="nokeyneeded",
2528
)
2629
MODEL_NAME = os.getenv("OLLAMA_MODEL")
30+
31+
elif API_HOST == "github":
32+
33+
client = openai.OpenAI(
34+
base_url="https://models.inference.ai.azure.com",
35+
api_key=os.getenv("GITHUB_TOKEN")
36+
)
37+
MODEL_NAME = os.getenv("GITHUB_MODEL")
38+
2739
else:
40+
2841
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
2942
MODEL_NAME = os.getenv("OPENAI_MODEL")
3043

chat_async.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@
2121
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
2222
elif API_HOST == "ollama":
2323
client = openai.AsyncOpenAI(
24-
base_url="http://localhost:11434/v1",
24+
base_url=os.getenv("OLLAMA_ENDPOINT"),
2525
api_key="nokeyneeded",
2626
)
2727
MODEL_NAME = os.getenv("OLLAMA_MODEL")
28+
elif API_HOST == "github":
29+
client = openai.AsyncOpenAI(
30+
base_url="https://models.inference.ai.azure.com",
31+
api_key=os.getenv("GITHUB_TOKEN")
32+
)
33+
MODEL_NAME = os.getenv("GITHUB_MODEL")
2834
else:
2935
client = openai.AsyncOpenAI(api_key=os.getenv("OPENAI_KEY"))
3036
MODEL_NAME = os.getenv("OPENAI_MODEL")

chat_history.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
2121
elif API_HOST == "ollama":
2222
client = openai.OpenAI(
23-
base_url="http://localhost:11434/v1",
23+
base_url=os.getenv("OLLAMA_ENDPOINT"),
2424
api_key="nokeyneeded",
2525
)
2626
MODEL_NAME = os.getenv("OLLAMA_MODEL")
27+
elif API_HOST == "github":
28+
client = openai.OpenAI(
29+
base_url="https://models.inference.ai.azure.com",
30+
api_key=os.getenv("GITHUB_TOKEN")
31+
)
32+
MODEL_NAME = os.getenv("GITHUB_MODEL")
2733
else:
2834
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
2935
MODEL_NAME = os.getenv("OPENAI_MODEL")

chat_history_stream.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
2121
elif API_HOST == "ollama":
2222
client = openai.OpenAI(
23-
base_url="http://localhost:11434/v1",
23+
base_url=os.getenv("OLLAMA_ENDPOINT"),
2424
api_key="nokeyneeded",
2525
)
2626
MODEL_NAME = os.getenv("OLLAMA_MODEL")
27+
elif API_HOST == "github":
28+
client = openai.OpenAI(
29+
base_url="https://models.inference.ai.azure.com",
30+
api_key=os.getenv("GITHUB_TOKEN")
31+
)
32+
MODEL_NAME = os.getenv("GITHUB_MODEL")
2733
else:
2834
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
2935
MODEL_NAME = os.getenv("OPENAI_MODEL")

chat_langchain.py

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
openai_api_base=os.getenv("OLLAMA_ENDPOINT"),
2626
openai_api_key=os.getenv("OPENAI_KEY"),
2727
)
28+
elif API_HOST == "github":
29+
llm = ChatOpenAI(
30+
model_name=os.getenv("GITHUB_MODEL"),
31+
openai_api_base="https://models.inference.ai.azure.com",
32+
openai_api_key=os.getenv("GITHUB_TOKEN"),
33+
)
2834
else:
2935
llm = ChatOpenAI(model_name=os.getenv("OPENAI_MODEL"), openai_api_key=os.getenv("OPENAI_KEY"))
3036

chat_llamaindex.py

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
llm = OpenAILike(
2828
model=os.getenv("OLLAMA_MODEL"), api_base=os.getenv("OLLAMA_ENDPOINT"), api_key="fake", is_chat_model=True
2929
)
30+
elif API_HOST == "github":
31+
llm = OpenAILike(
32+
model=os.getenv("GITHUB_MODEL"),
33+
api_base="https://models.inference.ai.azure.com",
34+
api_key=os.getenv("GITHUB_TOKEN"),
35+
is_chat_model=True)
3036
else:
3137
llm = OpenAI(model=os.getenv("OPENAI_MODEL"), api_key=os.getenv("OPENAI_KEY"))
3238

chat_safety.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
2121
elif API_HOST == "ollama":
2222
client = openai.OpenAI(
23-
base_url="http://localhost:11434/v1",
23+
base_url=os.getenv("OLLAMA_ENDPOINT"),
2424
api_key="nokeyneeded",
2525
)
2626
MODEL_NAME = os.getenv("OLLAMA_MODEL")
27+
elif API_HOST == "github":
28+
client = openai.OpenAI(
29+
base_url="https://models.inference.ai.azure.com",
30+
api_key=os.getenv("GITHUB_TOKEN")
31+
)
32+
MODEL_NAME = os.getenv("GITHUB_MODEL")
2733
else:
2834
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
2935
MODEL_NAME = os.getenv("OPENAI_MODEL")

chat_stream.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
api_key="nokeyneeded",
2525
)
2626
MODEL_NAME = os.getenv("OLLAMA_MODEL")
27+
elif API_HOST == "github":
28+
client = openai.OpenAI(
29+
base_url="https://models.inference.ai.azure.com",
30+
api_key=os.getenv("GITHUB_TOKEN")
31+
)
32+
MODEL_NAME = os.getenv("GITHUB_MODEL")
2733
else:
2834
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
2935
MODEL_NAME = os.getenv("OPENAI_MODEL")
@@ -45,4 +51,5 @@
4551
for event in completion:
4652
if event.choices:
4753
content = event.choices[0].delta.content
48-
print(content, end="", flush=True)
54+
if content:
55+
print(content, end="", flush=True)

main.py

-2
This file was deleted.

0 commit comments

Comments
 (0)