Skip to content

Commit

Permalink
Merge pull request #74 from cheahjs/fix/azure-openai
Browse files Browse the repository at this point in the history
fix: make azure openai work by dropping/modifying params
  • Loading branch information
tjbck authored Jun 7, 2024
2 parents 93cb893 + 19fef57 commit c062c5c
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions examples/pipelines/providers/azure_openai_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import List, Union, Generator, Iterator
from schemas import OpenAIChatMessage
from pydantic import BaseModel
import requests

Expand All @@ -10,9 +9,7 @@ class Valves(BaseModel):
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
API_VERSION: str = "2024-02-01"

def __init__(self):
# Optionally, you can set the id and name of the pipeline.
Expand All @@ -35,7 +32,7 @@ async def on_shutdown(self):
pass

def pipe(
self, user_message: str, model_id: str, messages: List[dict], body: dict
self, user_message: str, model_id: str, messages: List[dict], body: dict
) -> Union[str, Generator, Iterator]:
# This is where you can add your custom pipelines like RAG.
print(f"pipe:{__name__}")
Expand All @@ -50,10 +47,22 @@ def pipe(

url = f"{self.valves.AZURE_OPENAI_ENDPOINT}/openai/deployments/{self.valves.DEPLOYMENT_NAME}/chat/completions?api-version={self.valves.API_VERSION}"

allowed_params = {'messages', 'temperature', 'role', 'content', 'contentPart', 'contentPartImage',
'enhancements', 'dataSources', 'n', 'stream', 'stop', 'max_tokens', 'presence_penalty',
'frequency_penalty', 'logit_bias', 'user', 'function_call', 'funcions', 'tools',
'tool_choice', 'top_p', 'log_probs', 'top_logprobs', 'response_format', 'seed'}
# remap user field
if "user" in body and not isinstance(body["user"], str):
body["user"] = body["user"]["id"] if "id" in body["user"] else str(body["user"])
filtered_body = {k: v for k, v in body.items() if k in allowed_params}
# log fields that were filtered out as a single line
if len(body) != len(filtered_body):
print(f"Dropped params: {', '.join(set(body.keys()) - set(filtered_body.keys()))}")

try:
r = requests.post(
url=url,
json={**body, "model": self.valves.MODEL},
json=filtered_body,
headers=headers,
stream=True,
)
Expand All @@ -64,4 +73,8 @@ def pipe(
else:
return r.json()
except Exception as e:
return f"Error: {e}"
if r:
text = r.text
return f"Error: {e} ({text})"
else:
return f"Error: {e}"

0 comments on commit c062c5c

Please sign in to comment.