enh: azure openai pipeline

This commit is contained in:
Timothy J. Baek 2024-06-01 22:59:27 -07:00
parent f627142ccb
commit c4b5f2be47

View File

@ -1,9 +1,19 @@
from typing import List, Union, Generator, Iterator from typing import List, Union, Generator, Iterator
from schemas import OpenAIChatMessage from schemas import OpenAIChatMessage
from pydantic import BaseModel
import requests import requests
class Pipeline: class Pipeline:
class Valves(BaseModel):
# You can add your custom valves here.
AZURE_OPENAI_API_KEY: str = "your-azure-openai-api-key-here"
AZURE_OPENAI_ENDPOINT: str = "your-azure-openai-endpoint-here"
DEPLOYMENT_NAME: str = "your-deployment-name-here"
API_VERSION: str = "2023-10-01-preview"
MODEL: str = "gpt-3.5-turbo"
pass
def __init__(self): def __init__(self):
# Optionally, you can set the id and name of the pipeline. # Optionally, you can set the id and name of the pipeline.
# Assign a unique identifier to the pipeline. # Assign a unique identifier to the pipeline.
@ -11,6 +21,7 @@ class Pipeline:
# The identifier must be an alphanumeric string that can include underscores or hyphens. It cannot contain spaces, special characters, slashes, or backslashes. # The identifier must be an alphanumeric string that can include underscores or hyphens. It cannot contain spaces, special characters, slashes, or backslashes.
self.id = "azure_openai_pipeline" self.id = "azure_openai_pipeline"
self.name = "Azure OpenAI Pipeline" self.name = "Azure OpenAI Pipeline"
self.valves = self.Valves()
pass pass
async def on_startup(self): async def on_startup(self):
@ -32,25 +43,22 @@ class Pipeline:
print(messages) print(messages)
print(user_message) print(user_message)
AZURE_OPENAI_API_KEY = "your-azure-openai-api-key-here" headers = {
AZURE_OPENAI_ENDPOINT = "your-azure-openai-endpoint-here" "api-key": self.valves.AZURE_OPENAI_API_KEY,
DEPLOYMENT_NAME = "your-deployment-name-here" "Content-Type": "application/json",
MODEL = "gpt-3.5-turbo" }
headers = {"api-key": AZURE_OPENAI_API_KEY, "Content-Type": "application/json"} url = f"{self.valves.AZURE_OPENAI_ENDPOINT}/openai/deployments/{self.valves.DEPLOYMENT_NAME}/chat/completions?api-version={self.valves.API_VERSION}"
url = f"{AZURE_OPENAI_ENDPOINT}/openai/deployments/{DEPLOYMENT_NAME}/chat/completions?api-version=2023-10-01-preview"
try: try:
r = requests.post( r = requests.post(
url=url, url=url,
json={**body, "model": MODEL}, json={**body, "model": self.valves.MODEL},
headers=headers, headers=headers,
stream=True, stream=True,
) )
r.raise_for_status() r.raise_for_status()
if body["stream"]: if body["stream"]:
return r.iter_lines() return r.iter_lines()
else: else: