Skip to content

Instantly share code, notes, and snippets.

@Talismanic
Last active November 11, 2024 00:59
Show Gist options
  • Save Talismanic/720fd4d17a948cd4b79d465676c92a27 to your computer and use it in GitHub Desktop.
Save Talismanic/720fd4d17a948cd4b79d465676c92a27 to your computer and use it in GitHub Desktop.
Building search agent with camel-ai with the steps shown in camel-ai's tutorial docs.

Install Framework Dependencies

!pip install "camel-ai[all]==0.2.8"

!pip install 'camel-ai[tools]'

Define Behavior

sys_msg = `You are an avid searcher who can search and find anything on internet. You present the data in concise manner from your search result. At the end of your presentation, you add your life philosophy with the answer.`

error_msg = `It\'s not you, it\'s me.\nI failed to find the result for you.\nI will be better in future.`

Define Model

from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
from camel.configs import ChatGPTConfig


model = ModelFactory.create(
    model_platform=ModelPlatformType.OPENAI,
    model_type=ModelType.GPT_4O_MINI,
    model_config_dict=ChatGPTConfig().as_dict(), #  the config for model
)

Define the Agent

from camel.agents import ChatAgent agent = ChatAgent( system_message=sys_msg, model=model, message_window_size=10, # the length for chat memory )

Define the Task with Tools

from camel.toolkits import SearchToolkit
from camel.toolkits import FunctionTool

def search_on_internet(search_text):
    # define the tools where you want to search
    google_tool = FunctionTool(SearchToolkit().search_google)
    wiki_tool = FunctionTool(SearchToolkit().search_wiki)

    #define agent to use those tools
    agent = ChatAgent(
        system_message = sys_msg,
        tools = [
            google_tool,
            wiki_tool
        ]
    )

    # execute the search task
    try:
        response = agent.step(search_text)
        return response.msgs[0].content
    except:
        return error_msg

Run the Agent

question = input ('please type your question:\n')
print(search_on_internet(question))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment