This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
New model support #72
Open
lucifertrj
wants to merge
1
commit into
aiplanethub:main
Choose a base branch
from
lucifertrj:azurechatsupport
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from typing import Optional, Union, Any, Dict, Tuple | ||
from pydantic import Field | ||
|
||
from langchain.chat_models import AzureChatOpenAI | ||
|
||
from genai_stack.model.base import BaseModel, BaseModelConfig, BaseModelConfigModel | ||
|
||
class AzureChatOpenAIParameters(BaseModelConfigModel): | ||
model_name: str = Field(default="gpt-4-32k", alias="model") | ||
"""Model name to use.""" | ||
model_kwargs: Dict[str, Any] = Field(default_factory=dict) | ||
"""Holds any model parameters valid for create call not explicitly specified.""" | ||
deployment_name: str | ||
"""Deployment name created on Azure""" | ||
openai_api_key: str | ||
"""Base URL path for API requests, | ||
leave blank if not using a proxy or service emulator.""" | ||
temperature: float = 0 | ||
"""What sampling temperature to use.""" | ||
openai_api_base: Optional[str] = None | ||
openai_organization: Optional[str] = None | ||
# to support explicit proxy for OpenAI | ||
openai_proxy: Optional[str] = None | ||
request_timeout: Optional[Union[float, Tuple[float, float]]] = None | ||
"""Timeout for requests to OpenAI completion API. Default is 600 seconds.""" | ||
max_retries: int = 6 | ||
"""Maximum number of retries to make when generating.""" | ||
streaming: bool = False | ||
"""Whether to stream the results or not.""" | ||
n: int = 1 | ||
"""Number of chat completions to generate for each prompt.""" | ||
max_tokens: Optional[int] = None | ||
"""Maximum number of tokens to generate.""" | ||
tiktoken_model_name: Optional[str] = None | ||
"""The model name to pass to tiktoken when using this class. | ||
Tiktoken is used to count the number of tokens in documents to constrain | ||
them to be under a certain limit. By default, when set to None, this will | ||
be the same as the embedding model name. However, there are some cases | ||
where you may want to use this Embedding class with a model name not | ||
supported by tiktoken. This can include when using Azure embeddings or | ||
when using one of the many model providers that expose an OpenAI-like | ||
API but with different models. In those cases, in order to avoid erroring | ||
when tiktoken is called, you can specify a model name to use here.""" | ||
|
||
class AzureChatOpenAIModelConfigModel(BaseModelConfigModel): | ||
""" | ||
Data Model for the configs | ||
""" | ||
|
||
parameters: AzureChatOpenAIParameters | ||
|
||
class AzureChatOpenAIModelConfig(BaseModelConfig): | ||
data_model = AzureChatOpenAIModelConfigModel | ||
|
||
class AzureChatOpenAIModel(BaseModel): | ||
config_class = AzureChatOpenAIModelConfig | ||
|
||
def _post_init(self, *args, **kwargs): | ||
self.model = self.load() | ||
|
||
def load(self): | ||
""" | ||
Using dict method here to dynamically access object attributes | ||
""" | ||
model = AzureChatOpenAI( | ||
deployment_name= self.config.deployment_name, | ||
model_name = self.config.model_name, | ||
openai_api_type="azure", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove other parameters except openai_api_base & add
If you see you added the all parameters as config but you're not accepting that from users. |
||
model_kwargs=self.config.model_kwargs | ||
) | ||
return model | ||
|
||
def predict(self, prompt: str): | ||
response = self.model(prompt) | ||
return {"output": response[0]["generated_text"]} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the openai_api_base as it can be default as azure