Skip to content

Commit 235f703

Browse files
committed
implementing personalization with gorilla
1 parent 6e05036 commit 235f703

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OPENAI_KEY = "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"

go_cli.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import sys
2727
from halo import Halo
2828
import go_questionary
29-
29+
from utils import personalize
3030
__version__ = "0.0.11" # current version
3131
SERVER_URL = "https://cli.gorilla-llm.com"
3232
UPDATE_CHECK_FILE = os.path.expanduser("~/.gorilla-cli-last-update-check")
@@ -231,14 +231,23 @@ def get_history_commands(history_file):
231231
# Generate a unique interaction ID
232232
interaction_id = str(uuid.uuid4())
233233

234+
235+
personalized_input = f"""
236+
Some relevant context about my history:
237+
{personalize(user_input, get_history_commands(HISTORY_FILE), False)}
238+
239+
The query of the user is:
240+
{user_input}
241+
"""
242+
234243
if args.history:
235244
commands = get_history_commands(HISTORY_FILE)
236245
else:
237246
with Halo(text=f"{GORILLA_EMOJI}Loading", spinner="dots"):
238247
try:
239248
data_json = {
240249
"user_id": user_id,
241-
"user_input": user_input,
250+
"user_input": personalized_input,
242251
"interaction_id": interaction_id,
243252
"system_info": system_info
244253
}

utils.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import os
2+
from presidio_analyzer import AnalyzerEngine, PatternRecognizer
3+
from presidio_anonymizer import AnonymizerEngine
4+
from presidio_anonymizer.entities import OperatorConfig
5+
import json
6+
from pprint import pprint
7+
from openai import OpenAI
8+
9+
client = OpenAI(api_key=os.getenv("OPENAI_KEY"))
10+
11+
"""
12+
13+
1. Remove duplicates
14+
2. Make PI removal an optional flag
15+
16+
"""
17+
18+
19+
def get_bash_history():
20+
history_file = os.path.expanduser("~/.bash_history")
21+
prev_operations = ""
22+
try:
23+
with open(history_file, "r") as file:
24+
history = file.readlines()
25+
except FileNotFoundError:
26+
return "No bash history was found."
27+
return history[:-10]
28+
29+
30+
def anonymize_bash_history(operations):
31+
analyzer = AnalyzerEngine()
32+
analyzer_results = analyzer.analyze(text=operations, language="en")
33+
anonymizer = AnonymizerEngine()
34+
anonymized_results = anonymizer.anonymize(
35+
text=operations, analyzer_results=analyzer_results
36+
)
37+
return anonymized_results.text
38+
39+
40+
def remove_duplicates(operations: list[str]):
41+
return list(set(operations))
42+
43+
44+
def stringify_bash_history(operations: list[str]):
45+
return "\n".join(operations)
46+
47+
48+
def synthesize_bash_history(desired_operation, gorila_history, history):
49+
SYSTEM_PROMPT = """
50+
You are an assistant for a developer who wants to find the right API call for a specific task.
51+
The developer has bash history that contains the command they used to perform a task.
52+
Synthesize their bash history to provide the API call prediction model with extra context about the task.
53+
Use the previous bash history as well as their query to provide the model with a short paragraph of possible relevant context.
54+
There is a chance that their query has nothing to do with the bash history, so in that case, return 'No relevant context found'.
55+
"""
56+
USER_PROMPT = f"""
57+
The user's bash history is:
58+
{history}
59+
60+
The user's previous operations with the API calling tool are:
61+
{gorila_history}
62+
63+
The query of the user is:
64+
{desired_operation}
65+
66+
Use this information to provide the model with a short paragraph of possible relevant context.
67+
"""
68+
69+
response = client.chat.completions.create(
70+
model="gpt-4",
71+
messages=[
72+
{"role": "system", "content": SYSTEM_PROMPT},
73+
{"role": "user", "content": USER_PROMPT},
74+
],
75+
)
76+
return response.choices[0].message.content
77+
78+
79+
def personalize(query, gorilla_history, pi_removal=True):
80+
history = stringify_bash_history(remove_duplicates(get_bash_history()))
81+
if pi_removal:
82+
history = anonymize_bash_history(history)
83+
summary = synthesize_bash_history(query, gorilla_history, history)
84+
return summary

0 commit comments

Comments
 (0)