add extensive oi test

This commit is contained in:
ishaan-jaff 2023-09-16 11:27:22 -07:00
parent 296961a19b
commit d34b9f3688

View file

@ -401,14 +401,8 @@ def merge_deltas(original, delta):
original[key] = value
return original
def test_openai_openinterpreter_test():
try:
in_function_call = False
messages = [
{
'role': 'system',
'content': """You are Open Interpreter, a world-class programmer that can complete any goal by executing
system_message = """
You are Open Interpreter, a world-class programmer that can complete any goal by executing
code.\nFirst, write a plan. **Always recap the plan between each code block** (you have extreme short-term memory
loss, so you need to recap the plan between each message block to retain it).\nWhen you send a message containing
code to run_code, it will be executed **on the user's machine**. The user has given you **full and complete
@ -428,7 +422,15 @@ def test_openai_openinterpreter_test():
**it's critical not to try to do everything in one code block.** You should try something, print information about
it, then continue from there in tiny, informed steps. You will never get it on the first try, and attempting it in
one go will often lead to errors you cant see.\nYou are capable of **any** task.\n\n[User Info]\nName:
ishaanjaffer\nCWD: /Users/ishaanjaffer/Github/open-interpreter\nOS: Darwin"""
ishaanjaffer\nCWD: /Users/ishaanjaffer/Github/open-interpreter\nOS: Darwin
"""
def test_openai_openinterpreter_test():
try:
in_function_call = False
messages = [
{
'role': 'system',
'content': system_message
},
{'role': 'user', 'content': 'plot appl and nvidia on a graph'}
]
@ -464,6 +466,10 @@ def test_openai_openinterpreter_test():
new_messages.append({})
for chunk in response:
delta = chunk["choices"][0]["delta"]
finish_reason = chunk["choices"][0]["finish_reason"]
if finish_reason:
if finish_reason == "function_call":
assert(finish_reason == "function_call")
# Accumulate deltas into the last message in messages
new_messages[-1] = merge_deltas(new_messages[-1], delta)
@ -477,7 +483,50 @@ def test_openai_openinterpreter_test():
print(function_call)
assert("name" in function_call)
assert("arguments" in function_call)
# simulate running the function and getting output
new_messages.append({
"role": "function",
"name": "run_code",
"content": """'Traceback (most recent call last):\n File
"/Users/ishaanjaffer/Github/open-interpreter/interpreter/code_interpreter.py", line 183, in run\n code =
self.add_active_line_prints(code)\n File
"/Users/ishaanjaffer/Github/open-interpreter/interpreter/code_interpreter.py", line 274, in add_active_line_prints\n
return add_active_line_prints_to_python(code)\n File
"/Users/ishaanjaffer/Github/open-interpreter/interpreter/code_interpreter.py", line 442, in
add_active_line_prints_to_python\n tree = ast.parse(code)\n File
"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 50, in parse\n return
compile(source, filename, mode, flags,\n File "<unknown>", line 1\n !pip install pandas yfinance matplotlib\n
^\nSyntaxError: invalid syntax\n'
"""})
# make 2nd gpt-4 call
print("\n2nd completion call\n")
response = completion(
model="gpt-4",
messages=[ {'role': 'system','content': system_message} ] + new_messages,
functions=function_schema,
temperature=0,
stream=True,
)
new_messages.append({})
for chunk in response:
delta = chunk["choices"][0]["delta"]
finish_reason = chunk["choices"][0]["finish_reason"]
if finish_reason:
if finish_reason == "function_call":
assert(finish_reason == "function_call")
# Accumulate deltas into the last message in messages
new_messages[-1] = merge_deltas(new_messages[-1], delta)
print(new_messages)
print("new messages after merge_delta", new_messages)
assert("function_call" in new_messages[-1]) # ensure this call has a function_call in response
assert(new_messages[0]['role'] == 'user')
assert(new_messages[1]['role'] == 'assistant')
function_call = new_messages[-1]['function_call']
print(function_call)
assert("name" in function_call)
assert("arguments" in function_call)
except Exception as e:
pytest.fail(f"Error occurred: {e}")
test_openai_openinterpreter_test()
# test_openai_openinterpreter_test()