Update Azure OpenAI to new OpenAI API spec
This commit is contained in:
@@ -27,7 +27,7 @@ pip install path_to_this_repo
|
||||
Tou can run the simple sample like so:
|
||||
|
||||
```
|
||||
src/samples/simple-sample/simple-sample.py -u [YOUR_ROOM_URL] -k [YOUR_DAILY_API_KEY]
|
||||
src/samples/simple-sample/simple-sample.py -u your_room_url -k your_daily_api_key
|
||||
```
|
||||
|
||||
Note that the sample uses Azure's TTS and LLM services. You'll need to set the following environment variables for the sample to work:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
import io
|
||||
import openai
|
||||
from openai import AzureOpenAI
|
||||
|
||||
import os
|
||||
import requests
|
||||
|
||||
@@ -45,17 +46,20 @@ class AzureTTSService(TTSService):
|
||||
self.logger.info("Error details: {}".format(cancellation_details.error_details))
|
||||
|
||||
class AzureLLMService(LLMService):
|
||||
def get_response(self, messages, stream):
|
||||
return openai.ChatCompletion.create(
|
||||
api_type="azure",
|
||||
api_version="2023-06-01-preview",
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.client = AzureOpenAI(
|
||||
api_key=os.getenv("AZURE_CHATGPT_KEY"),
|
||||
api_base=os.getenv("AZURE_CHATGPT_ENDPOINT"),
|
||||
deployment_id=os.getenv("AZURE_CHATGPT_DEPLOYMENT_ID"),
|
||||
stream=stream,
|
||||
messages=messages,
|
||||
azure_endpoint=os.getenv("AZURE_CHATGPT_ENDPOINT"),
|
||||
api_version="2023-12-01-preview",
|
||||
)
|
||||
|
||||
def get_response(self, messages, stream):
|
||||
return self.client.chat.completions.create(
|
||||
stream=stream,
|
||||
messages=messages,
|
||||
model=os.getenv("AZURE_CHATGPT_DEPLOYMENT_ID"),
|
||||
)
|
||||
|
||||
def run_llm_async(self, messages) -> Generator[str, None, None]:
|
||||
local_messages = messages.copy()
|
||||
@@ -65,15 +69,11 @@ class AzureLLMService(LLMService):
|
||||
response = self.get_response(local_messages, stream=True)
|
||||
|
||||
for chunk in response:
|
||||
if len(chunk["choices"]) == 0:
|
||||
if len(chunk.choices) == 0:
|
||||
continue
|
||||
|
||||
if "content" in chunk["choices"][0]["delta"]:
|
||||
if (
|
||||
chunk["choices"][0]["delta"]["content"] != {}
|
||||
): # streaming a content chunk
|
||||
yield chunk["choices"][0]["delta"]["content"]
|
||||
|
||||
if chunk.choices[0].delta.content:
|
||||
yield chunk.choices[0].delta.content
|
||||
|
||||
def run_llm(self, messages) -> str | None:
|
||||
local_messages = messages.copy()
|
||||
@@ -81,13 +81,8 @@ class AzureLLMService(LLMService):
|
||||
self.logger.debug(f"Generating chat via azure: {messages_for_log}")
|
||||
|
||||
response = self.get_response(local_messages, stream=False)
|
||||
if (
|
||||
response
|
||||
and len(response["choices"]) > 0
|
||||
and "message" in response["choices"][0]
|
||||
and "content" in response["choices"][0]["message"]
|
||||
):
|
||||
return response["choices"][0]["message"]["content"]
|
||||
if response and len(response.choices) > 0:
|
||||
return response.choices[0].message.content
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -97,13 +92,13 @@ class AzureImageGenService(ImageGenService):
|
||||
def run_image_gen(self, sentence) -> tuple[str, Image.Image]:
|
||||
self.logger.info("Generating azure image", sentence)
|
||||
|
||||
image = openai.Image.create(
|
||||
api_type = 'azure',
|
||||
api_version = '2023-06-01-preview',
|
||||
api_key = os.getenv('AZURE_DALLE_KEY'),
|
||||
api_base = os.getenv('AZURE_DALLE_ENDPOINT'),
|
||||
deployment_id = os.getenv("AZURE_DALLE_DEPLOYMENT_ID"),
|
||||
prompt=f'{sentence} in the style of {self.image_style}',
|
||||
image = OpenAI().images.generate(
|
||||
api_type="azure",
|
||||
api_version="2023-06-01-preview",
|
||||
api_key=os.getenv("AZURE_DALLE_KEY"),
|
||||
api_base=os.getenv("AZURE_DALLE_ENDPOINT"),
|
||||
deployment_id=os.getenv("AZURE_DALLE_DEPLOYMENT_ID"),
|
||||
prompt=f"{sentence} in the style of {self.image_style}",
|
||||
n=1,
|
||||
size=f"1024x1024",
|
||||
)
|
||||
|
||||
@@ -2,7 +2,9 @@ from services.ai_service import AIService
|
||||
import requests
|
||||
from PIL import Image
|
||||
import io
|
||||
import openai
|
||||
from openai import OpenAI
|
||||
|
||||
client = OpenAI()
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
@@ -21,15 +23,13 @@ class OpenAIService(AIService):
|
||||
model = os.getenv("OPEN_AI_MODEL")
|
||||
if not model:
|
||||
model = "gpt-4"
|
||||
response = openai.ChatCompletion.create(
|
||||
api_type = 'openai',
|
||||
api_version = '2020-11-07',
|
||||
api_base = "https://api.openai.com/v1",
|
||||
api_key = os.getenv("OPEN_AI_KEY"),
|
||||
model=model,
|
||||
stream=stream,
|
||||
messages=local_messages
|
||||
)
|
||||
response = client.chat.completions.create(api_type = 'openai',
|
||||
api_version = '2020-11-07',
|
||||
api_base = "https://api.openai.com/v1",
|
||||
api_key = os.getenv("OPEN_AI_KEY"),
|
||||
model=model,
|
||||
stream=stream,
|
||||
messages=local_messages)
|
||||
|
||||
return response
|
||||
|
||||
@@ -37,15 +37,13 @@ class OpenAIService(AIService):
|
||||
self.logger.info("🖌️ generating openai image async for ", sentence)
|
||||
start = time.time()
|
||||
|
||||
image = openai.Image.create(
|
||||
api_type = 'openai',
|
||||
api_version = '2020-11-07',
|
||||
api_base = "https://api.openai.com/v1",
|
||||
api_key = os.getenv("OPEN_AI_KEY"),
|
||||
prompt=f'{sentence} in the style of {self.image_style}',
|
||||
n=1,
|
||||
size=f"1024x1024",
|
||||
)
|
||||
image = client.images.generate(api_type = 'openai',
|
||||
api_version = '2020-11-07',
|
||||
api_base = "https://api.openai.com/v1",
|
||||
api_key = os.getenv("OPEN_AI_KEY"),
|
||||
prompt=f'{sentence} in the style of {self.image_style}',
|
||||
n=1,
|
||||
size=f"1024x1024")
|
||||
image_url = image["data"][0]["url"]
|
||||
self.logger.info("🖌️ generated image from url", image["data"][0]["url"])
|
||||
response = requests.get(image_url)
|
||||
|
||||
Reference in New Issue
Block a user