-
Notifications
You must be signed in to change notification settings - Fork 14
OpenAI Action #577
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
Closed
Closed
OpenAI Action #577
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7107,3 +7107,130 @@ def download_chrome_extension(data_set): | |
| except Exception: | ||
| return CommonUtil.Exception_Handler(sys.exc_info()) | ||
|
|
||
|
|
||
| @logger | ||
| def AI_LLM_prompt_with_files(data_set): | ||
| """ | ||
| This action will extract the text from images using OpenAI's vision API. This action also takes user prompt and returns | ||
| the result according to the user prompt. If the user does not give any prompt, then by default it | ||
| extracts all text from the image and returns the result in JSON format. | ||
|
|
||
| Args: | ||
| data_set: | ||
| ------------------------------------------------------------------------------ | ||
| image | input parameter | %| image.png |% | ||
| user prompt | optional parameter | Extract invoice details | ||
| AI - LLM prompt with files | common action | AI - LLM prompt with files | ||
| ------------------------------------------------------------------------------ | ||
|
|
||
| Return: | ||
| `passed` if success | ||
| `zeuz_failed` if fails | ||
| """ | ||
| sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME | ||
| global selenium_driver | ||
|
|
||
| try: | ||
| import base64 | ||
| import requests | ||
| import json | ||
| import os | ||
| user_image_path = None | ||
| user_prompt = None | ||
|
|
||
| for left, mid, right in data_set: | ||
| left = left.lower().replace(" ", "") | ||
| mid = mid.lower().replace(" ", "") | ||
| right = right.strip() | ||
|
|
||
| if left == 'image': | ||
| if right != '': | ||
| user_image_path = right | ||
|
|
||
| if left == "userprompt": | ||
| if right != '': | ||
| user_prompt = right | ||
|
|
||
| # Validate image path | ||
| if not user_image_path: | ||
| CommonUtil.ExecLog(sModuleInfo, "No image path provided. Please provide an image path.", 3) | ||
| return "zeuz_failed" | ||
|
|
||
| image_path = user_image_path | ||
| CommonUtil.ExecLog(sModuleInfo, f"Processing image: {image_path}", 1) | ||
|
|
||
| if not os.path.isfile(image_path): | ||
| CommonUtil.ExecLog(sModuleInfo, f"Image file not found: {image_path}", 3) | ||
| return "zeuz_failed" | ||
|
|
||
| prompt = user_prompt | ||
| if not prompt: | ||
| prompt = "Extract all text from this image and return the result in JSON format." | ||
|
|
||
| # Convert Image to Base64 | ||
| with open(image_path, "rb") as img_file: | ||
| base64_image = base64.b64encode(img_file.read()).decode("utf-8") | ||
|
|
||
| # Load API key from .env file | ||
| try: | ||
| from dotenv import load_dotenv | ||
| framework_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) | ||
| env_path = os.path.join(framework_dir, ".env") | ||
| load_dotenv(env_path) | ||
| api_key = os.getenv("OPENAI_API") | ||
| if not api_key: | ||
| CommonUtil.ExecLog(sModuleInfo, "OPENAI_API not found in .env file", 3) | ||
| return "zeuz_failed" | ||
| except Exception as e: | ||
| CommonUtil.ExecLog(sModuleInfo, f"Failed to load API key from .env: {str(e)}", 3) | ||
| return "zeuz_failed" | ||
|
|
||
| # Prepare API Request | ||
| headers = { | ||
| "Authorization": f"Bearer {api_key}", | ||
| "Content-Type": "application/json" | ||
| } | ||
|
|
||
| payload = { | ||
| "model": "gpt-4o", | ||
|
Member
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. Users should be able to specify the model name as part of the action parameters. |
||
| "messages": [ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| { | ||
| "type": "image_url", | ||
| "image_url": { | ||
| "url": f"data:image/png;base64,{base64_image}" | ||
| } | ||
| }, | ||
| { | ||
| "type": "text", | ||
| "text": prompt | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| # Send Request | ||
| CommonUtil.ExecLog(sModuleInfo, "Analyzing image...", 1) | ||
| response = requests.post( | ||
| "https://api.openai.com/v1/chat/completions", | ||
| headers=headers, | ||
| data=json.dumps(payload) | ||
| ) | ||
|
|
||
| # === 5. Process Response === | ||
| if response.status_code == 200: | ||
| response_data = response.json() | ||
| extracted_data = response_data["choices"][0]["message"]["content"] | ||
| CommonUtil.ExecLog(sModuleInfo, f"Text extracted successfully from: {image_path}", 1) | ||
| CommonUtil.ExecLog(sModuleInfo, f"Extracted content: {extracted_data}", 5) | ||
| return "passed" | ||
| else: | ||
| CommonUtil.ExecLog(sModuleInfo, f"OpenAI API error: {response.status_code} - {response.text}", 3) | ||
| return "zeuz_failed" | ||
|
|
||
| except Exception: | ||
| return CommonUtil.Exception_Handler(sys.exc_info()) | ||
|
|
||
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.
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.
I don't think we need to load these ourselves.
load_dotenv()is already called whennode_cli.pystarts. So, if a user specifies and environment variable and starts node, it'll already be available inos.getenv()calls.