diff --git a/guidance/models/_azure_openai.py b/guidance/models/_azure_openai.py index 9b06edd74..afa804311 100644 --- a/guidance/models/_azure_openai.py +++ b/guidance/models/_azure_openai.py @@ -1,5 +1,4 @@ import pathlib -import re from typing import Type from urllib.parse import parse_qs, urlparse @@ -11,8 +10,7 @@ from ._openai import ( OpenAIChatEngine, OpenAICompletionEngine, - OpenAIInstructEngine, - chat_model_pattern, + OpenAIInstructEngine ) try: @@ -73,18 +71,15 @@ def __init__( if api_key is None and azure_ad_token_provider is None: raise ValueError("Please provide either api_key or azure_ad_token_provider") - parsed_url = urlparse(azure_endpoint) - # if we are called directly (as opposed to through super()) then we convert ourselves to # a more specific subclass if possible if self.__class__ is AzureOpenAI: # Default to a completion model - found_subclass: Type[AzureOpenAI] = AzureOpenAICompletion - # Now see if we should be using a chat model - if parsed_url.path.endswith("/chat/completions"): - found_subclass = AzureOpenAIChat - elif re.match(chat_model_pattern, model): - found_subclass = AzureOpenAIChat + found_subclass: Type[AzureOpenAI] = ( + AzureOpenAICompletion + if model.endswith("-instruct") + else AzureOpenAIChat + ) # convert to any found subclass self.__class__ = found_subclass @@ -101,9 +96,14 @@ def __init__( **kwargs, ) return + + parsed_url = urlparse(azure_endpoint) if azure_deployment is None: - azure_deployment = pathlib.Path(parsed_url.path).parts[3] + parts = pathlib.Path(parsed_url.path).parts + if len(parts) > 2: + azure_deployment = parts[3] + parsed_query = parse_qs(parsed_url.query) api_version = ( version @@ -159,4 +159,4 @@ def get_role_end(self, name): else: raise ValueError( f"The OpenAIInstruct model does not know about the {name} role type!" - ) + ) \ No newline at end of file diff --git a/guidance/models/_openai.py b/guidance/models/_openai.py index 2a0c60f5b..33632f9ef 100644 --- a/guidance/models/_openai.py +++ b/guidance/models/_openai.py @@ -1,5 +1,4 @@ import os -import re import typing import diskcache as dc @@ -18,8 +17,6 @@ except ImportError: client_class = None -chat_model_pattern = r"^(ft:)?(gpt-3\.5-turbo|gpt-4)(?:(?!-instruct$)(-\w+)+)?(:[\w-]+(?:[:\w-]+)*)?(::\w+)?$" - class OpenAIEngine(GrammarlessEngine): def __init__( @@ -92,19 +89,16 @@ def __init__( # if we are called directly (as opposed to through super()) then we convert ourselves to a more specific subclass if possible if self.__class__ is OpenAI: - found_subclass = None - - # chat - if re.match(chat_model_pattern, model): - found_subclass = OpenAIChat # instruct # elif "instruct" in model: # All current OpenAI instruct models behave as Completion models. # found_subclass = OpenAIInstruct - - # regular completion - else: - found_subclass = OpenAICompletion + + found_subclass: typing.Type[OpenAI] = ( + OpenAICompletion + if model.endswith("-instruct") + else OpenAIChat + ) # convert to any found subclass self.__class__ = found_subclass