-
Notifications
You must be signed in to change notification settings - Fork 79
Implementing personalization with Gorilla #56
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
235f703
implementing personalization with gorilla
arthbohra 95df7f4
small prompt adjustment
arthbohra 8e64981
updating gitignore
arthbohra 7aadbae
fix: enabling users to decide whether they want to personalize
arthbohra 1136fc8
added configurations file to save personalization settings
arthbohra 306ff50
refactored entire code for cleanliness
arthbohra 6cfa5bb
small cleanups
arthbohra 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ dist | |
| .vscode | ||
| .idea | ||
| .editorconfig | ||
| .env | ||
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,106 @@ | ||
| import os | ||
| from presidio_analyzer import AnalyzerEngine, PatternRecognizer | ||
| from presidio_anonymizer import AnonymizerEngine | ||
| from presidio_anonymizer.entities import OperatorConfig | ||
| import json | ||
| from pprint import pprint | ||
| from openai import OpenAI | ||
| from personalization.prompts import get_system_prompt, get_user_prompt | ||
|
|
||
| class GorillaPersonalizer: | ||
| """A class to personalize the user's bash history and query to provide the model with relevant context. | ||
|
|
||
| Attributes: | ||
| client: The OpenAI client. | ||
|
|
||
| """ | ||
|
|
||
| def __init__(self, open_ai_key): | ||
| """ Initializes the GorillaPersonalizer class.""" | ||
| self.client = OpenAI(api_key=open_ai_key) | ||
|
|
||
| def get_bash_history(self): | ||
| """ | ||
| Retrieves the user's bash history. (Last 10 commands) | ||
| """ | ||
| history_file = os.path.expanduser("~/.bash_history") | ||
| prev_operations = "" | ||
| try: | ||
| with open(history_file, "r") as file: | ||
| history = file.readlines() | ||
| except FileNotFoundError: | ||
| return "No bash history was found." | ||
| return history[:-10] | ||
|
|
||
| def anonymize_bash_history(self, operations): | ||
| """ | ||
| Uses Microsoft's Presidio to anonymize the user's bash history. | ||
|
|
||
| Args: | ||
| operations: The user's bash history. | ||
|
|
||
| """ | ||
| analyzer = AnalyzerEngine() | ||
| analyzer_results = analyzer.analyze(text=operations, language="en") | ||
| anonymizer = AnonymizerEngine() | ||
| anonymized_results = anonymizer.anonymize( | ||
| text=operations, analyzer_results=analyzer_results | ||
| ) | ||
| return anonymized_results.text | ||
|
|
||
| def remove_duplicates(self, operations: list[str]): | ||
| """Removes duplicates from the user's bash history | ||
|
|
||
| Args: | ||
| operations: The user's bash history. | ||
|
|
||
| """ | ||
| return list(set(operations)) | ||
|
|
||
| def stringify_bash_history(self, operations: list[str]): | ||
| """Stringifies the user's bash history. | ||
|
|
||
| Args: | ||
| operations: The user's bash history. | ||
|
|
||
| """ | ||
| return "\n".join(operations) | ||
|
|
||
| def synthesize_bash_history(self, desired_operation, gorilla_history, history): | ||
| """Uses OpenAI api to synthesize the user's bash, gorilla history. | ||
| It synthesizes this history with the current operation in mind, with the goal of providing the model with relevant context. | ||
|
|
||
| Args: | ||
| client: The OpenAI client. | ||
| desired_operation: The operation the user wants to perform. | ||
| gorilla_history: The user's previous operations with the Gorilla | ||
| history: The user's bash history. | ||
|
|
||
| """ | ||
| system_prompt = get_system_prompt() | ||
| user_prompt = get_user_prompt(history, gorilla_history, desired_operation) | ||
| response = self.client.chat.completions.create( | ||
| model="gpt-4", | ||
| messages=[ | ||
| {"role": "system", "content": system_prompt}, | ||
| {"role": "user", "content": user_prompt}, | ||
| ], | ||
| ) | ||
| return response.choices[0].message.content | ||
|
|
||
| def personalize(self, query, gorilla_history, pi_removal=True): | ||
| """Personalizes the user's bash history and query to provide the model with relevant context. | ||
|
|
||
| Args: | ||
| query: The operation the user wants to perform. | ||
| gorila_history: The user's previous operations with the Gorilla | ||
| open_ai_key: The OpenAI API key. | ||
| pi_removal: Whether to remove personally identifiable information from the user's bash history. | ||
|
|
||
| """ | ||
|
|
||
| history = self.stringify_bash_history(self.remove_duplicates(self.get_bash_history())) | ||
| if pi_removal: | ||
| history = self.anonymize_bash_history(history) | ||
| summary = self.synthesize_bash_history(query, gorilla_history, history) | ||
| return summary |
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,26 @@ | ||
|
|
||
|
|
||
| def get_system_prompt(): | ||
| return f""" | ||
| You are an assistant for a developer who wants to find the right API call for a specific task. | ||
| The developer has bash history that contains the command they used to perform a task. | ||
| Synthesize their bash history to provide the API call prediction model with extra context about the task. | ||
| For reference, the API call prediction model, called Gorilla, is trained on a large dataset of API calls and their associated tasks. | ||
| You may see the developer's previous operations with the API calling tool in their bash history. | ||
| Use the previous bash history as well as their query to provide the model with a short paragraph of possible relevant context. | ||
| There is a chance that their query has nothing to do with the bash history, so in that case, return 'No relevant context found'. | ||
| """ | ||
|
|
||
| def get_user_prompt(history, gorilla_history, desired_operation): | ||
| return f""" | ||
| The user's bash history is: | ||
| {history} | ||
|
|
||
| The user's previous operations with the API calling tool are: | ||
| {gorilla_history} | ||
|
|
||
| The query of the user is: | ||
| {desired_operation} | ||
|
|
||
| Us this information to provide the model with a short paragraph of possible relevant context. | ||
| """ |
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.