DEV Community

Md Shahriar
Md Shahriar

Posted on

Learning to build AI Agents(Part 1)

I've recently been exploring AI agents, so here is a quick guide to get started.

First thing needed to learn is Python fundamentals such as lists, dictionaries and sets. Python data can be serialized into JSON when communicating with APIs. Python lists correspond to JSON arrays, whereas dictionaries correspond to JSON objects. Gemini can send information in either text or structured JSON. But response.text gets us a Python string containing the generated text, when available. Moreover, a good thing to learn would be sets, as they help store unique values that have already been processed, which can ultimately help increase efficiency by avoiding duplicate work. Moreover, it is good practice to learn about the try/except concept because there will be times when the API you're trying to access might fail to return an answer, and it could break the code. But it is important to know that empty responses don't always raise exceptions.

The next thing to learn is about APIs (Application Programming Interfaces), which allow different applications to communicate with each other. A lot of the time, the application you're trying to access will have an API that provides information you can use. Setting up access to an API is relatively simple—obtain an API key from the application and store it securely in an environment variable or a .env file. Make sure that you avoid uploading the key in a public space because if someone uses your key, you could be charged for it. The API key can then be accessed in the main Python file using the os module, although some SDKs, including Gemini's, can read the environment variable automatically. However, it is key to remember to read the API documentation of the website that you're about to use. Different applications have different ways of communicating with others. Underneath, many of them use HTTP and JSON, but it is important to look for the requirements of that API.

chat = client.chats.create(
    model="YOUR_AVAILABLE_MODEL"
)

response = chat.send_message("Hello!")

print(response.text)
Enter fullscreen mode Exit fullscreen mode

The code above is how you interact with Gemini. It uses Google's own Python SDK, which is simpler to manage than making HTTP API requests manually. client is the Gemini SDK client object that is used to communicate with Gemini using your API key. The chat object is used to maintain the conversation history of your chat session. model is the Gemini model, and you need to specify a model that is available to you (which can be found in the API docs).

chat = client.chats.create(
    model="YOUR_AVAILABLE_MODEL",
    config=types.GenerateContentConfig(
        tools=[check_inventory]
    )
)
Enter fullscreen mode Exit fullscreen mode

The above code is another way to create a chat session with Gemini. The way Gemini generates a response can then be configured using the GenerateContentConfig class, which is predefined in the types module. It can be used to configure the behaviour of the response that Gemini produces. One of the features of configuring the response is that we can pass a function without the parentheses so that Gemini can request that particular function when needed to gather information about a query. So Gemini decides whether to call the function, the Python SDK executes the code locally when automatic function calling is enabled, and sends the returned value back to Gemini.

Top comments (0)