-
Notifications
You must be signed in to change notification settings - Fork 13
Add openai as separate model provider #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
20a3784
Add openai as separate model provider
cd46ee6
Add test for OpenAI provider instantiation in MicroBot
070a2e0
Add AZURE_OPENAI_API_VERSION environment variable and validation checks
2c2f52f
Add validation tests for AzureOpenAIApi initialization errors
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,99 @@ | ||
| import json | ||
| import os | ||
| from collections.abc import Callable | ||
| from dataclasses import asdict | ||
|
|
||
| from dotenv import load_dotenv | ||
| from openai import AzureOpenAI | ||
| from microbots.llm.llm import LLMAskResponse, LLMInterface | ||
|
|
||
| load_dotenv() | ||
|
|
||
| endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") | ||
| api_version = os.getenv("AZURE_OPENAI_API_VERSION") | ||
| deployment_name = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME") | ||
| api_key = os.getenv("AZURE_OPENAI_API_KEY") | ||
|
|
||
|
|
||
| class AzureOpenAIApi(LLMInterface): | ||
|
|
||
| def __init__(self, system_prompt, deployment_name=deployment_name, max_retries=3, | ||
| token_provider: Callable[[], str] | None = None): | ||
| self.token_provider = token_provider | ||
|
|
||
| if not endpoint: | ||
| raise ValueError( | ||
| "AZURE_OPENAI_ENDPOINT environment variable is required when using Azure OpenAI. " | ||
| "Set it to your Azure OpenAI resource endpoint (e.g. 'https://<resource>.openai.azure.com/')." | ||
| ) | ||
|
|
||
| if not api_version: | ||
| raise ValueError( | ||
| "AZURE_OPENAI_API_VERSION environment variable is required when using Azure OpenAI. " | ||
| "Set it to a valid API version (e.g. '2024-12-01-preview')." | ||
| ) | ||
|
|
||
| if not token_provider and not api_key: | ||
| raise ValueError( | ||
| "No authentication configured for Azure OpenAI. Either set the AZURE_OPENAI_API_KEY " | ||
| "environment variable or provide a token_provider (e.g. AzureTokenProvider)." | ||
| ) | ||
|
|
||
| if token_provider: | ||
| if not callable(token_provider): | ||
| raise ValueError("token_provider must be a callable that returns a string token.") | ||
| try: | ||
| token = token_provider() | ||
| except Exception as e: | ||
| raise ValueError(f"token_provider failed during validation: {e}") from e | ||
| if not isinstance(token, str) or not token: | ||
| raise ValueError("token_provider must return a non-empty string token.") | ||
| self.ai_client = AzureOpenAI( | ||
| azure_endpoint=endpoint, | ||
| azure_ad_token_provider=token_provider, | ||
| api_version=api_version, | ||
| ) | ||
| else: | ||
| # Azure OpenAI with API key | ||
| self.ai_client = AzureOpenAI( | ||
| azure_endpoint=endpoint, | ||
| api_key=api_key, | ||
| api_version=api_version, | ||
| ) | ||
| self.deployment_name = deployment_name | ||
| self.system_prompt = system_prompt | ||
| self.messages = [{"role": "system", "content": system_prompt}] | ||
|
|
||
| # Set these values here. This logic will be handled in the parent class. | ||
| self.max_retries = max_retries | ||
| self.retries = 0 | ||
|
|
||
| def ask(self, message) -> LLMAskResponse: | ||
| self.retries = 0 # reset retries for each ask. Handled in parent class. | ||
|
|
||
| self.messages.append({"role": "user", "content": message}) | ||
|
|
||
| valid = False | ||
| while not valid: | ||
| response = self.ai_client.responses.create( | ||
| model=self.deployment_name, | ||
| input=self.messages, | ||
| ) | ||
| self.messages.append({"role": "assistant", "content": response.output_text}) | ||
| valid, askResponse = self._validate_llm_response(response=response.output_text) | ||
|
|
||
| # Remove last assistant message and replace with structured response | ||
| self.messages.pop() | ||
| self.messages.append({"role": "assistant", "content": json.dumps(asdict(askResponse))}) | ||
|
|
||
| return askResponse | ||
|
|
||
| def clear_history(self): | ||
| self.messages = [ | ||
| { | ||
| "role": "system", | ||
| "content": self.system_prompt, | ||
| } | ||
| ] | ||
| return True | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.