camel-ai / camel

🐫 CAMEL: Finding the Scaling Law of Agents. A multi-agent framework. https://www.camel-ai.org
https://docs.camel-ai.org/
Apache License 2.0
5.54k stars 676 forks source link

[Feature Request] `ChatAgent` add code_execution capability #622

Closed Wendong-Fan closed 4 months ago

Wendong-Fan commented 4 months ago

Required prerequisites

Motivation

each chat agent should be able to execute the code rather than only the EmbodiedAgent

Solution

transfer capability from EmbodiedAgent into ChatAgent, and remove EmbodiedAgent

enable_code_execution setting can be ‘auto’, ‘True’, ‘False’, need more research to check how OpenAI did

config reference: code_execution_config{work_dir (Optional, str), use_docker (Optional, list, str or bool), timeout (Optional, int), last_n_messages (Experimental, int or str) #The number of messages to look back for code execution}

Alternatives

No response

Additional context

No response

onemquan commented 4 months ago

OpenAI code-interpreter Case

from openai import OpenAI
client = OpenAI()

assistant = client.beta.assistants.create(
    # instructions: The system instructions that the assistant uses.
    instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.",
    model="gpt-3.5-turbo",
    temperature=0.0,
    tools=[{"type": "code_interpreter"}],
)

question = "Weng earns $12 an hour for babysitting. Yesterday, she just did 50 minutes of babysitting. How much did she earn?"

thread = client.beta.threads.create(
    messages=[
        {
            "role": "user",
            "content": question,
        }
    ]
)
run = client.beta.threads.runs.create_and_poll(
    thread_id=thread.id,
    assistant_id=assistant.id,
)

run_steps = client.beta.threads.runs.steps.list(
    thread_id=thread.id, run_id=run.id
)

for step in run_steps:
    print(step.step_details)

# MessageCreationStepDetails(message_creation=MessageCreation(message_id='msg_qnRfCmnCHoi0P5rh8JArdiBU'), type='message_creation')
# ToolCallsStepDetails(tool_calls=[CodeInterpreterToolCall(id='call_tAICvobwplzPZ6eZbjxAaqeq', code_interpreter=CodeInterpreter(input='# Constants\nhourly_rate = 12\nminutes_worked = 50\n\n# Convert minutes to hours\nhours_worked = minutes_worked / 60\n\n# Calculate earnings\nearnings = hours_worked * hourly_rate\nearnings', outputs=[]), type='code_interpreter')], type='tool_calls')
# MessageCreationStepDetails(message_creation=MessageCreation(message_id='msg_dQl0XqodCDfgkheVeazPcO25'), type='message_creation')
Wendong-Fan commented 4 months ago

Maybe we can also set code executor as one tool. @WHALEEYE is working on adding docker for the code execution to make it more safe, once the PR is created maybe @onemquan can also help do the review

cc @lightaime