-
Notifications
You must be signed in to change notification settings - Fork 74
added AzureOpenAI API call #11
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,211 @@ | ||||||||||||||||||||||||||||||
| # add streaming | ||||||||||||||||||||||||||||||
| from openai import AsyncAzureOpenAI | ||||||||||||||||||||||||||||||
| from openai import AzureOpenAI | ||||||||||||||||||||||||||||||
| from dotenv import load_dotenv | ||||||||||||||||||||||||||||||
| import asyncio | ||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||
| import time | ||||||||||||||||||||||||||||||
| from .llm_response_models import LLMFullResponse,LLMEmbeddingsResponse | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Load environment variables | ||||||||||||||||||||||||||||||
| load_dotenv() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| MAX_RETRIES = int(os.getenv("MAX_RETRIES", 3)) | ||||||||||||||||||||||||||||||
| RETRY_DELAY = int(os.getenv("RETRY_DELAY", 2)) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def generate_response( | ||||||||||||||||||||||||||||||
| model_name, | ||||||||||||||||||||||||||||||
| messages=None, | ||||||||||||||||||||||||||||||
| temperature=0.7, | ||||||||||||||||||||||||||||||
| max_tokens=300, | ||||||||||||||||||||||||||||||
| top_p=1.0, | ||||||||||||||||||||||||||||||
| full_response=False, | ||||||||||||||||||||||||||||||
| api_key = None, | ||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||
| start_time = time.time() if full_response else None | ||||||||||||||||||||||||||||||
| openai_client = AzureOpenAI(api_key=api_key, | ||||||||||||||||||||||||||||||
| api_version="2024-07-01-preview", | ||||||||||||||||||||||||||||||
| azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT") | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for attempt in range(MAX_RETRIES): | ||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||
| completion = openai_client.chat.completions.create( | ||||||||||||||||||||||||||||||
| model=model_name, | ||||||||||||||||||||||||||||||
| messages=messages, | ||||||||||||||||||||||||||||||
| temperature=temperature, | ||||||||||||||||||||||||||||||
| max_tokens=max_tokens, | ||||||||||||||||||||||||||||||
| top_p=top_p, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| generated_text = completion.choices[0].message.content | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if full_response: | ||||||||||||||||||||||||||||||
| end_time = time.time() | ||||||||||||||||||||||||||||||
| process_time = end_time - start_time | ||||||||||||||||||||||||||||||
| return LLMFullResponse( | ||||||||||||||||||||||||||||||
| generated_text=generated_text, | ||||||||||||||||||||||||||||||
| model=model_name, | ||||||||||||||||||||||||||||||
| process_time=process_time, | ||||||||||||||||||||||||||||||
| llm_provider_response=completion, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| return generated_text | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||
| if attempt < MAX_RETRIES - 1: | ||||||||||||||||||||||||||||||
| time.sleep(RETRY_DELAY * (2**attempt)) | ||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||
| error_msg = f"Failed after {MAX_RETRIES} attempts" | ||||||||||||||||||||||||||||||
| if full_response: | ||||||||||||||||||||||||||||||
| end_time = time.time() | ||||||||||||||||||||||||||||||
| process_time = end_time - start_time | ||||||||||||||||||||||||||||||
| error_msg += f" and {process_time} seconds" | ||||||||||||||||||||||||||||||
| error_msg += f" due to: {e}" | ||||||||||||||||||||||||||||||
| print(error_msg) | ||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||
|
Comment on lines
+54
to
+65
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. 🛠️ Refactor suggestion Improve exception handling and use proper logging. Currently, errors are being printed using Apply the following changes:
Example diff: +import logging
...
54 except Exception as e:
55 if attempt < MAX_RETRIES - 1:
56 time.sleep(RETRY_DELAY * (2**attempt))
57 else:
58 error_msg = f"Failed after {MAX_RETRIES} attempts"
59 if full_response:
60 end_time = time.time()
61 process_time = end_time - start_time
62 error_msg += f" and {process_time} seconds"
63 error_msg += f" due to: {e}"
-64 print(error_msg)
-65 return None
+64 logging.error(error_msg)
+65 raise Exception(error_msg)
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async def generate_response_async( | ||||||||||||||||||||||||||||||
| model_name, | ||||||||||||||||||||||||||||||
| messages=None, | ||||||||||||||||||||||||||||||
| temperature=0.7, | ||||||||||||||||||||||||||||||
| max_tokens=300, | ||||||||||||||||||||||||||||||
| top_p=1.0, | ||||||||||||||||||||||||||||||
| full_response=False, | ||||||||||||||||||||||||||||||
| api_key = None, | ||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||
| start_time = time.time() if full_response else None | ||||||||||||||||||||||||||||||
| async_openai_client = AsyncAzureOpenAI(api_key=api_key, | ||||||||||||||||||||||||||||||
| api_version="2024-07-01-preview", | ||||||||||||||||||||||||||||||
| azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT") | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for attempt in range(MAX_RETRIES): | ||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||
| completion = await async_openai_client.chat.completions.create( | ||||||||||||||||||||||||||||||
| model=model_name, | ||||||||||||||||||||||||||||||
| messages=messages, | ||||||||||||||||||||||||||||||
| temperature=temperature, | ||||||||||||||||||||||||||||||
| max_tokens=max_tokens, | ||||||||||||||||||||||||||||||
| top_p=top_p, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| generated_text = completion.choices[0].message.content | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if full_response: | ||||||||||||||||||||||||||||||
| end_time = time.time() | ||||||||||||||||||||||||||||||
| process_time = end_time - start_time | ||||||||||||||||||||||||||||||
| return LLMFullResponse( | ||||||||||||||||||||||||||||||
| generated_text=generated_text, | ||||||||||||||||||||||||||||||
| model=model_name, | ||||||||||||||||||||||||||||||
| process_time=process_time, | ||||||||||||||||||||||||||||||
| llm_provider_response=completion, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| return generated_text | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||
| if attempt < MAX_RETRIES - 1: | ||||||||||||||||||||||||||||||
| await asyncio.sleep(RETRY_DELAY * (2**attempt)) | ||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||
| error_msg = f"Failed after {MAX_RETRIES} attempts" | ||||||||||||||||||||||||||||||
| if full_response: | ||||||||||||||||||||||||||||||
| end_time = time.time() | ||||||||||||||||||||||||||||||
| process_time = end_time - start_time | ||||||||||||||||||||||||||||||
| error_msg += f" and {process_time} seconds" | ||||||||||||||||||||||||||||||
| error_msg += f" due to: {e}" | ||||||||||||||||||||||||||||||
| print(error_msg) | ||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
Comment on lines
+67
to
+116
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. 🛠️ Refactor suggestion Enhance error handling in The asynchronous function Apply similar changes as suggested for
|
||||||||||||||||||||||||||||||
| def generate_embeddings( | ||||||||||||||||||||||||||||||
| model_name, | ||||||||||||||||||||||||||||||
| user_input=None, | ||||||||||||||||||||||||||||||
| full_response = False, | ||||||||||||||||||||||||||||||
| api_key = None | ||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||
|
Comment on lines
+117
to
+122
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. Add input validation for The Apply this diff to add validation: 117 def generate_embeddings(
118 model_name,
119 user_input=None,
120 full_response=False,
121 api_key=None
122 ):
+ if not model_name:
+ raise ValueError("model_name must be provided and cannot be empty.")📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if not user_input: | ||||||||||||||||||||||||||||||
| raise ValueError("user_input must be provided.") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| start_time = time.time() if full_response else None | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| openai_client = AzureOpenAI(api_key=api_key, | ||||||||||||||||||||||||||||||
| api_version="2024-07-01-preview", | ||||||||||||||||||||||||||||||
| azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT") | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for attempt in range(MAX_RETRIES): | ||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| response = openai_client.embeddings.create( | ||||||||||||||||||||||||||||||
| model= model_name, | ||||||||||||||||||||||||||||||
| input=user_input | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| generate_embeddings = response.data | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if full_response: | ||||||||||||||||||||||||||||||
| end_time = time.time() | ||||||||||||||||||||||||||||||
| process_time = end_time - start_time | ||||||||||||||||||||||||||||||
| return LLMEmbeddingsResponse( | ||||||||||||||||||||||||||||||
| generated_embedding=generate_embeddings, | ||||||||||||||||||||||||||||||
| model=model_name, | ||||||||||||||||||||||||||||||
| process_time=process_time, | ||||||||||||||||||||||||||||||
| llm_provider_response=response, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| return generate_embeddings | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||
| if attempt < MAX_RETRIES - 1: | ||||||||||||||||||||||||||||||
| time.sleep(RETRY_DELAY * (2**attempt)) | ||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||
| error_msg = f"Failed after {MAX_RETRIES} attempts" | ||||||||||||||||||||||||||||||
| if full_response: | ||||||||||||||||||||||||||||||
| end_time = time.time() | ||||||||||||||||||||||||||||||
| process_time = end_time - start_time | ||||||||||||||||||||||||||||||
| error_msg += f" and {process_time} seconds" | ||||||||||||||||||||||||||||||
| error_msg += f" due to: {e}" | ||||||||||||||||||||||||||||||
| print(error_msg) | ||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||
|
Comment on lines
+154
to
+165
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. 🛠️ Refactor suggestion Improve exception handling and use proper logging in Similar to previous suggestions, replace Apply the following changes: +import logging
...
154 except Exception as e:
155 if attempt < MAX_RETRIES - 1:
156 time.sleep(RETRY_DELAY * (2**attempt))
157 else:
158 error_msg = f"Failed after {MAX_RETRIES} attempts"
159 if full_response:
160 end_time = time.time()
161 process_time = end_time - start_time
162 error_msg += f" and {process_time} seconds"
163 error_msg += f" due to: {e}"
-164 print(error_msg)
-165 return None
+164 logging.error(error_msg)
+165 raise Exception(error_msg)
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async def generate_embeddings_async( | ||||||||||||||||||||||||||||||
| model_name, | ||||||||||||||||||||||||||||||
| user_input=None, | ||||||||||||||||||||||||||||||
| full_response = False, | ||||||||||||||||||||||||||||||
| api_key = None, | ||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||
|
Comment on lines
+167
to
+172
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. Add input validation for Similar to the synchronous version, ensure that Apply this diff: 167 async def generate_embeddings_async(
168 model_name,
169 user_input=None,
170 full_response=False,
171 api_key=None,
172 ):
+ if not model_name:
+ raise ValueError("model_name must be provided and cannot be empty.")📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
| async_openai_client = AsyncAzureOpenAI(api_key=api_key, | ||||||||||||||||||||||||||||||
| api_version="2024-07-01-preview", | ||||||||||||||||||||||||||||||
| azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT") | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| if not user_input: | ||||||||||||||||||||||||||||||
| raise ValueError("user_input must be provided.") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| start_time = time.time() if full_response else None | ||||||||||||||||||||||||||||||
| for attempt in range(MAX_RETRIES): | ||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||
| result = await async_openai_client.embeddings.create( | ||||||||||||||||||||||||||||||
| model=model_name, | ||||||||||||||||||||||||||||||
| input=user_input, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| generate_embeddings = result.data | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if full_response: | ||||||||||||||||||||||||||||||
| end_time = time.time() | ||||||||||||||||||||||||||||||
| process_time = end_time - start_time | ||||||||||||||||||||||||||||||
| return LLMEmbeddingsResponse( | ||||||||||||||||||||||||||||||
| generated_embedding=generate_embeddings, | ||||||||||||||||||||||||||||||
| model=model_name, | ||||||||||||||||||||||||||||||
| process_time=process_time, | ||||||||||||||||||||||||||||||
| llm_provider_response=result, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| return generate_embeddings | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||
| if attempt < MAX_RETRIES - 1: | ||||||||||||||||||||||||||||||
| await asyncio.sleep(RETRY_DELAY * (2**attempt)) | ||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||
| error_msg = f"Failed after {MAX_RETRIES} attempts" | ||||||||||||||||||||||||||||||
| if full_response: | ||||||||||||||||||||||||||||||
| end_time = time.time() | ||||||||||||||||||||||||||||||
| process_time = end_time - start_time | ||||||||||||||||||||||||||||||
| error_msg += f" and {process_time} seconds" | ||||||||||||||||||||||||||||||
| error_msg += f" due to: {e}" | ||||||||||||||||||||||||||||||
| print(error_msg) | ||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||
|
Comment on lines
+200
to
+211
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. 🛠️ Refactor suggestion Enhance error handling in Align the asynchronous embeddings function with the error handling practices used elsewhere:
Apply the following changes: +import logging
...
200 except Exception as e:
201 if attempt < MAX_RETRIES - 1:
202 await asyncio.sleep(RETRY_DELAY * (2**attempt))
203 else:
204 error_msg = f"Failed after {MAX_RETRIES} attempts"
205 if full_response:
206 end_time = time.time()
207 process_time = end_time - start_time
208 error_msg += f" and {process_time} seconds"
209 error_msg += f" due to: {e}"
-210 print(error_msg)
-211 return None
+210 logging.error(error_msg)
+211 raise Exception(error_msg)
|
||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -47,6 +47,9 @@ VALUE_SERP_API_KEY="your_value_serp_api_key_here" #for Google search | |||||||||||||||||||||||||||||
| SERPER_API_KEY="your_serper_api_key_here" #for Google search | ||||||||||||||||||||||||||||||
| STABILITY_API_KEY="your_stability_api_key_here" #for image generation | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| AZUREOPENAI_API_KEY="your_azureopenai_api_key_here" | ||||||||||||||||||||||||||||||
| AZURE_OPENAI_ENDPOINT='your_azureopenai_endpoint_url_here' | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
Comment on lines
+50
to
+52
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. 🛠️ Refactor suggestion Standardize environment variable naming and add deployment name. The Azure OpenAI configuration needs some adjustments:
-AZUREOPENAI_API_KEY="your_azureopenai_api_key_here"
+AZURE_OPENAI_API_KEY="your_azure_openai_api_key_here"
AZURE_OPENAI_ENDPOINT='your_azureopenai_endpoint_url_here'
+AZURE_OPENAI_DEPLOYMENT_NAME='your_azure_openai_deployment_name_here'📝 Committable suggestion
Suggested change
🛠️ Refactor suggestion Add Azure OpenAI usage example in code samples. The environment variables are added but there's no example showing how to use Azure OpenAI in the "Creating an LLM Instance" section. Add this example to the code samples section: # For Anthropic Claude
#llm_instance = LLM.create(LLMProvider.ANTHROPIC, model_name="claude-3-opus-20240229")
+
+# For Azure OpenAI
+#llm_instance = LLM.create(provider=LLMProvider.AZURE_OPENAI, model_name="your-deployment-model")
response = llm_instance.generate_response(prompt="generate a 5 words sentence")📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| ### Creating an LLM Instance | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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.
💡 Codebase verification
Azure OpenAI API version needs to be updated to a stable version
The code currently uses the preview version "2024-07-01-preview" which is a future date and appears to be incorrect. According to Azure OpenAI's current versioning scheme, you should use a stable version like "2023-05-15" or check the Azure OpenAI documentation for the latest stable version.
SimplerLLM/language/llm_providers/azureopenai_llm.py, update theapi_versionin all four instances where it's set to "2024-07-01-preview"🔗 Analysis chain
Verify the correctness of the
api_version.The
api_versionis set to"2024-07-01-preview". Please confirm that this is the correct API version for Azure OpenAI. The latest available version might be different, which could affect compatibility.You can run the following script to list available API versions:
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 181
Script:
Length of output: 82
Script:
Length of output: 41
Script:
Length of output: 2224
Script:
Length of output: 7566
Script:
Length of output: 2219