-
Notifications
You must be signed in to change notification settings - Fork 80
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 3 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,86 @@ | ||
| import os | ||
| from presidio_analyzer import AnalyzerEngine, PatternRecognizer | ||
|
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. Don't you also need to include this in the requirements
Author
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. Where would I access the file for this? |
||
| from presidio_anonymizer import AnonymizerEngine | ||
| from presidio_anonymizer.entities import OperatorConfig | ||
| import json | ||
| from pprint import pprint | ||
| from openai import OpenAI | ||
|
|
||
| client = OpenAI(api_key=os.getenv("OPENAI_KEY")) | ||
|
arthbohra marked this conversation as resolved.
Outdated
|
||
|
|
||
| """ | ||
|
|
||
| 1. Remove duplicates | ||
| 2. Make PI removal an optional flag | ||
|
|
||
| """ | ||
|
|
||
|
|
||
| def get_bash_history(): | ||
| 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(operations): | ||
| 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(operations: list[str]): | ||
| return list(set(operations)) | ||
|
|
||
|
|
||
| def stringify_bash_history(operations: list[str]): | ||
| return "\n".join(operations) | ||
|
|
||
|
|
||
| def synthesize_bash_history(desired_operation, gorila_history, history): | ||
| SYSTEM_PROMPT = """ | ||
| 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'. | ||
| """ | ||
| USER_PROMPT = f""" | ||
| The user's bash history is: | ||
| {history} | ||
|
|
||
| The user's previous operations with the API calling tool are: | ||
| {gorila_history} | ||
|
|
||
| The query of the user is: | ||
| {desired_operation} | ||
|
|
||
| Use this information to provide the model with a short paragraph of possible relevant context. | ||
| """ | ||
|
|
||
| response = 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(query, gorilla_history, pi_removal=True): | ||
| history = stringify_bash_history(remove_duplicates(get_bash_history())) | ||
| if pi_removal: | ||
| history = anonymize_bash_history(history) | ||
| summary = synthesize_bash_history(query, gorilla_history, history) | ||
| return summary | ||
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.