-
Notifications
You must be signed in to change notification settings - Fork 19
Migrate to latest openai code #13
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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import json | ||
| import io | ||
| import openai | ||
| from openai import OpenAI | ||
|
|
||
| client = OpenAI() | ||
|
Contributor
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. This appears to only be used from |
||
| import os | ||
| import requests | ||
|
|
||
|
|
@@ -49,30 +51,26 @@ def run_llm(self, messages, stream = True): | |
| messages_for_log = json.dumps(messages) | ||
| self.logger.error(f"==== generating chat via azure openai: {messages_for_log}") | ||
|
|
||
| response = openai.ChatCompletion.create( | ||
| api_type = 'azure', | ||
| api_version = '2023-06-01-preview', | ||
| 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 | ||
| ) | ||
| response = client.chat.completions.create(api_type = 'azure', | ||
|
Contributor
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. Suggest just cleaning up the indentation here for readability (and below) |
||
| api_version = '2023-06-01-preview', | ||
| 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) | ||
| return response | ||
|
|
||
| def run_image_gen(self, sentence): | ||
| 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}', | ||
| n=1, | ||
| size=f"1024x1024", | ||
| ) | ||
| image = client.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") | ||
|
|
||
| url = image["data"][0]["url"] | ||
| response = requests.get(url) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,10 @@ | |
| import requests | ||
| from PIL import Image | ||
| import io | ||
| import openai | ||
| import os | ||
| from openai import OpenAI | ||
|
|
||
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | ||
|
Contributor
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. This appears to only be used from |
||
| import os | ||
| import time | ||
| import json | ||
|
|
@@ -18,14 +21,11 @@ def run_llm(self, messages, stream = True): | |
| 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=messages | ||
|
|
||
| response = client.chat.completions.create( | ||
| messages=messages, | ||
| model="gpt-4", | ||
| stream=stream | ||
| ) | ||
|
|
||
| return response | ||
|
|
@@ -34,15 +34,13 @@ def run_image_gen(self, sentence): | |
| 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"), | ||
|
Contributor
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. If we specify the API key when constructing the client, I'd presume this might no longer be needed. (let's clean up the indentation as well!) |
||
| 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) | ||
|
|
||
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.
This is more a note for us to follow up on as I realize it is not in scope of this PR (so more for @chadbailey59 and @Moishe maybe!), but the repeated list and property access here is redundant - I suggest, in a follow-up PR, accessing the required elements once for reuse (here and below). e.g.:
(Hastily written just to give an idea of what I mean)