-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
[WIP] ChatGPT data enrichment #2392
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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 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,45 @@ | ||
"""Chat GPT module for MobSF.""" | ||
import logging | ||
|
||
import openai | ||
|
||
from django.conf import settings | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ChatGPT: | ||
|
||
def __init__(self, api_key): | ||
self.gpt_client = openai.Client(api_key=api_key) | ||
self.gpt_model = settings.OPENAI_GPT_MODEL | ||
|
||
def get_available_models(self): | ||
models = set() | ||
for i in self.gpt_client.models.list(): | ||
models.add(i.id) | ||
return models | ||
|
||
def chat(self, messages): | ||
"""Chat with GPT.""" | ||
try: | ||
response = self.gpt_client.chat.completions.create( | ||
messages=messages, | ||
temperature=0, | ||
model=self.gpt_model, | ||
n=1, | ||
) | ||
return response.choices[0].message.content | ||
except openai.APIConnectionError: | ||
logger.error('The server could not be reached') | ||
except openai.RateLimitError: | ||
logger.error('You\'ve hit the OpenAI API rate limit') | ||
except openai.NotFoundError: | ||
logger.error('The requested model %s is not available. Available models: %s', self.gpt_model, self.get_available_models()) | ||
except openai.APIStatusError as e: | ||
logger.error('OpenAI API is returning an error') | ||
logger.error(e.status_code) | ||
logger.error(e.response) | ||
except Exception: | ||
logger.exception('Chat with GPT failed') | ||
return None |
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
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
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
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
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,53 @@ | ||
"""Chat GPT prompts for MobSF.""" | ||
import logging | ||
from pathlib import Path | ||
|
||
from django.conf import settings | ||
|
||
from mobsf.MobSF.chat_gpt import ChatGPT | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class AndroidPrompts: | ||
|
||
def __init__(self): | ||
if not settings.OPENAI_API_KEY: | ||
return None | ||
self.gpt = ChatGPT(settings.OPENAI_API_KEY) | ||
|
||
def shared_object_identifier(self, shared_objects): | ||
"""Identify shared object.""" | ||
return None | ||
list_of_shared_objects = list(shared_objects) | ||
messages = [ | ||
{'role': 'system', 'content': ( | ||
'You are analyzing shared object files for Android applications as a Static Analyzer.' | ||
'You must always be truthful. Your responses should always be in json format.')}, | ||
{'role': 'user', 'content': ( | ||
f'Identify the SDK or Company from the shared object files is used in the following list. {list_of_shared_objects}.' | ||
'The resulting json response should be a list of dicts with two keys the file_name and company_name.' | ||
'The output should not be broken, and must be a valid json.')}, | ||
] | ||
return self.gpt.chat(messages) | ||
|
||
def package_name_identifier(self, source_dir): | ||
"""Identify package name.""" | ||
packages = set() | ||
src = Path(source_dir) | ||
for file_path in src.glob('**/*'): | ||
if file_path.is_file(): | ||
pkg = file_path.parent.relative_to(src).as_posix().replace('/', '.') | ||
packages.add(pkg) | ||
print(packages) | ||
messages = [ | ||
{'role': 'system', 'content': ( | ||
'You are analyzing Android application java source code as a Static Analyzer.' | ||
'You must always be truthful. Your responses should always be in json format.')}, | ||
{'role': 'user', 'content': ( | ||
f'Identify the library or SDK name from the following package names. {source_dir}.' | ||
'Ignore the package name if you cannot identify the library or SDK name.' | ||
'The resulting json response should be a list of dict with the keys library_name and package_name.' | ||
'The output should not be broken, and must be a valid json.')}, | ||
] | ||
return self.gpt.chat(messages) |
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.
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High