Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GitHub models #7

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/python-3
{
"name": "Python 3",
"name": "python-openai-demos (Azure OpenAI)",
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",

"features": {
Expand All @@ -14,10 +14,7 @@
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python",
"files.exclude": {
"__pycache__": true
}
"python.defaultInterpreterPath": "/usr/local/bin/python"
},

// Add the IDs of extensions you want installed when the container is created.
Expand All @@ -29,9 +26,6 @@
}
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pip3 install --user -r requirements-dev.txt",

Expand Down
31 changes: 31 additions & 0 deletions .devcontainer/github/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/python-3
{
"name": "python-openai-demos (GitHub models)",
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",
"features": {
},
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python"
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python",
"charliermarsh.ruff",
"ms-python.black-formatter"
]
}
},

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pip3 install --user -r requirements-dev.txt && cp .env.sample.github .env",

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
32 changes: 32 additions & 0 deletions .devcontainer/ollama/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/python-3
{
"name": "python-openai-demos (Ollama)",
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",
"features": {
"ghcr.io/prulloac/devcontainer-features/ollama:1": {}
},
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python"
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python",
"charliermarsh.ruff",
"ms-python.black-formatter"
]
}
},

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

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
29 changes: 29 additions & 0 deletions .devcontainer/openai/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/python-3
{
"name": "python-openai-demos (Ollama)",
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python"
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python",
"charliermarsh.ruff",
"ms-python.black-formatter"
]
}
},

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pip3 install --user -r requirements-dev.txt && cp .env.sample.openai .env",

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
6 changes: 4 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# API_HOST can be either azure, ollama, or openai:
# API_HOST can be either azure, ollama, openai, or github:
API_HOST=azure
# Needed for Azure:
AZURE_OPENAI_ENDPOINT=https://YOUR-AZURE-OPENAI-SERVICE-NAME.openai.azure.com
AZURE_OPENAI_DEPLOYMENT=YOUR-AZURE-DEPLOYMENT-NAME
AZURE_OPENAI_VERSION=2024-03-01-preview
# Needed for Ollama:
OLLAMA_ENDPOINT=http://localhost:11434/v1
OLLAMA_MODEL=llama2
OLLAMA_MODEL=llama3.1
# Needed for OpenAI.com:
OPENAI_KEY=YOUR-OPENAI-KEY
OPENAI_MODEL=gpt-3.5-turbo
# Needed for GitHub models:
GITHUB_MODEL=gpt-4o
5 changes: 5 additions & 0 deletions .env.sample.azure
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# See .env.sample for all options
API_HOST=azure
AZURE_OPENAI_ENDPOINT=https://YOUR-AZURE-OPENAI-SERVICE-NAME.openai.azure.com
AZURE_OPENAI_DEPLOYMENT=YOUR-AZURE-DEPLOYMENT-NAME
AZURE_OPENAI_VERSION=2024-03-01-preview
3 changes: 3 additions & 0 deletions .env.sample.github
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# See .env.sample for all options
API_HOST=github
GITHUB_MODEL=gpt-4o
4 changes: 4 additions & 0 deletions .env.sample.ollama
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# See .env.sample for all options
API_HOST=ollama
OLLAMA_ENDPOINT=http://localhost:11434/v1
OLLAMA_MODEL=llama3.1
4 changes: 4 additions & 0 deletions .env.sample.openai
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# See .env.sample for all options
API_HOST=openai
OPENAI_KEY=YOUR-OPENAI-KEY
OPENAI_MODEL=gpt-3.5-turbo
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.exclude": {
"__pycache__": true
}
}
10 changes: 10 additions & 0 deletions chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
API_HOST = os.getenv("API_HOST")

if API_HOST == "azure":

token_provider = azure.identity.get_bearer_token_provider(
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
Expand All @@ -18,13 +19,22 @@
azure_ad_token_provider=token_provider,
)
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")

elif API_HOST == "ollama":

client = openai.OpenAI(
base_url=os.getenv("OLLAMA_ENDPOINT"),
api_key="nokeyneeded",
)
MODEL_NAME = os.getenv("OLLAMA_MODEL")

elif API_HOST == "github":

client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
MODEL_NAME = os.getenv("GITHUB_MODEL")

else:

client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
MODEL_NAME = os.getenv("OPENAI_MODEL")

Expand Down
5 changes: 4 additions & 1 deletion chat_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
elif API_HOST == "ollama":
client = openai.AsyncOpenAI(
base_url="http://localhost:11434/v1",
base_url=os.getenv("OLLAMA_ENDPOINT"),
api_key="nokeyneeded",
)
MODEL_NAME = os.getenv("OLLAMA_MODEL")
elif API_HOST == "github":
client = openai.AsyncOpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
MODEL_NAME = os.getenv("GITHUB_MODEL")
else:
client = openai.AsyncOpenAI(api_key=os.getenv("OPENAI_KEY"))
MODEL_NAME = os.getenv("OPENAI_MODEL")
Expand Down
5 changes: 4 additions & 1 deletion chat_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
elif API_HOST == "ollama":
client = openai.OpenAI(
base_url="http://localhost:11434/v1",
base_url=os.getenv("OLLAMA_ENDPOINT"),
api_key="nokeyneeded",
)
MODEL_NAME = os.getenv("OLLAMA_MODEL")
elif API_HOST == "github":
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
MODEL_NAME = os.getenv("GITHUB_MODEL")
else:
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
MODEL_NAME = os.getenv("OPENAI_MODEL")
Expand Down
5 changes: 4 additions & 1 deletion chat_history_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
elif API_HOST == "ollama":
client = openai.OpenAI(
base_url="http://localhost:11434/v1",
base_url=os.getenv("OLLAMA_ENDPOINT"),
api_key="nokeyneeded",
)
MODEL_NAME = os.getenv("OLLAMA_MODEL")
elif API_HOST == "github":
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
MODEL_NAME = os.getenv("GITHUB_MODEL")
else:
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
MODEL_NAME = os.getenv("OPENAI_MODEL")
Expand Down
6 changes: 6 additions & 0 deletions chat_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
openai_api_base=os.getenv("OLLAMA_ENDPOINT"),
openai_api_key=os.getenv("OPENAI_KEY"),
)
elif API_HOST == "github":
llm = ChatOpenAI(
model_name=os.getenv("GITHUB_MODEL"),
openai_api_base="https://models.inference.ai.azure.com",
openai_api_key=os.getenv("GITHUB_TOKEN"),
)
else:
llm = ChatOpenAI(model_name=os.getenv("OPENAI_MODEL"), openai_api_key=os.getenv("OPENAI_KEY"))

Expand Down
7 changes: 7 additions & 0 deletions chat_llamaindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
llm = OpenAILike(
model=os.getenv("OLLAMA_MODEL"), api_base=os.getenv("OLLAMA_ENDPOINT"), api_key="fake", is_chat_model=True
)
elif API_HOST == "github":
llm = OpenAILike(
model=os.getenv("GITHUB_MODEL"),
api_base="https://models.inference.ai.azure.com",
api_key=os.getenv("GITHUB_TOKEN"),
is_chat_model=True,
)
else:
llm = OpenAI(model=os.getenv("OPENAI_MODEL"), api_key=os.getenv("OPENAI_KEY"))

Expand Down
5 changes: 4 additions & 1 deletion chat_safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
elif API_HOST == "ollama":
client = openai.OpenAI(
base_url="http://localhost:11434/v1",
base_url=os.getenv("OLLAMA_ENDPOINT"),
api_key="nokeyneeded",
)
MODEL_NAME = os.getenv("OLLAMA_MODEL")
elif API_HOST == "github":
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
MODEL_NAME = os.getenv("GITHUB_MODEL")
else:
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
MODEL_NAME = os.getenv("OPENAI_MODEL")
Expand Down
6 changes: 5 additions & 1 deletion chat_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
api_key="nokeyneeded",
)
MODEL_NAME = os.getenv("OLLAMA_MODEL")
elif API_HOST == "github":
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
MODEL_NAME = os.getenv("GITHUB_MODEL")
else:
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
MODEL_NAME = os.getenv("OPENAI_MODEL")
Expand All @@ -45,4 +48,5 @@
for event in completion:
if event.choices:
content = event.choices[0].delta.content
print(content, end="", flush=True)
if content:
print(content, end="", flush=True)
2 changes: 0 additions & 2 deletions main.py

This file was deleted.