How to Create a Simple AI Function Agent Using Groq API

Uploaded on 2024-09-14

How to Create a Simple AI Function Agent Using Groq API
  1. Introduction

Today, I’ll show you how to build a simple AI Function Agent using the Groq API. This agent can understand what you want it to do, like opening websites, searching, and more. By the end, your agent will return Python code snippets to perform the tasks you ask. Let's break it down step by step!


Step 1: Get Your Groq API Key

First things first, to talk to the Groq AI, you’ll need an API key. Follow these steps:


Step 2: Install Required Libraries

Before you write the code, make sure you have the Groq library installed. You can install it using the following command in your terminal:


   pip install groq


copy


Step 3: Define System Messages

We need to tell the AI what its job is. This is where the system messages come in. In this project, the AI will act like a Function Agent, figuring out tasks and returning them in JSON format.

Here's a basic message structure that you’ll use:



messages = [

    {

        "role": "system",

        "content":  """YOU are a Function Agent that understands user queries and determines if the query is related to opening, closing, searching, and other actions. You should always return your response in JSON format like this:

        DEMO OF USER TELLING TO OPEN WEBSITE:

        {

            "function_name": "Open Website",

            "target_entity": {

                "type": "Website",

                "name": "Specify the website or app name here"

            },

            "action_details": {

                "description": "Provide a modified and clear version of the user's query here",

                "user_query": "Original user query here",

                "is_action_identified": true,

                "action_type": "Open"

            },

            "response": "The action requested by the user has been successfully completed. For example, the website 'Specify the website or app name here' has been opened as per the user's request.",

            "code_snippet": {

                "language": "Python",

                "code": "import webbrowser\nwebbrowser.open('target_link_of_site')"

            }

        }

        """

    }

]


copy


This message will guide the AI on how to respond when you ask it something like “open YouTube.” It will always return its answer in JSON format and provide a Python code snippet that does the task.


Step 4: Write the Python Code

Now it’s time to write the code to connect to the Groq API. Here's what the code will do:

                        • Use the Groq API to talk to the AI.
                        • Understand the user's query.
                        • Return a Python code snippet that matches the user's request (like opening a website).


How the Code Works

Finally, here’s the full code. Paste your Groq API key where it says "YOUR_API_KEY_HERE".

Full Code:



from groq import Groq
import rejson

# Initialize Groq client with api key
client = Groq(api_key=groq_api_key)

# Initial system message to guide the AI agent
messages = [
    {
        
"role""system",
        
"content":  """YOU are a Function Agent that understands user queries and
 determines if the query is related to opening, closing, searching, and other actions.
 You should always return your response in JSON format like this:
        DEMO OF USER TELLING TO OPEN WEBSITE:
        {
            "function_name": "Open Website",
            "target_entity": {
                "type": "Website",
                "name": "Specify the website or app name here" 
           },
            "action_details": { 
               "description": "Provide a modified and clear version of the user's query here",
                "user_query": "Original user query here",
                "is_action_identified": true,
                "action_type": "Open"
            },
            "response": "The action requested by the user has been successfully completed. For example, the website 'Specify the website or app name here' has been opened as per the user's request.",
            "code_snippet": {
                "language": "Python",
                "code": "import webbrowser
\nwebbrowser.open('target_link_of_site') *provide\n for new line also try not to create variables. Write code in single line use \n to give enter now!! after importing modules as well*""
            }
        }

        YOU MUST: Provide JSON only, not any other response NOW!!        """
    }
]

# Function to interact with AI
def Ai(prompt):
    
add_messages("user"prompt)
    chat_completion 
= client.chat.completions.create(
        
messages=messages,
        
model="llama3-70b-8192"
    )
    response 
= chat_completion.choices[0].message.content
    
add_messages("assistant", response)

    
return response

# Helper function to add messages to the conversation history
def add_messages(rolecontent):
    messages.
append({"role"role"content"content})

# Main interaction loop
while True:
        user_query 
= input(">> ")
        res_json 
= Ai(user_query)

        
# Clean up potential problematic characters
        # This might help if there are invalid control characters
        res_json = re.sub(r'[\x00-\x1F]''', res_json) 

        
# Attempt to parse the string into a JSON dictionary
        res = json.loads(res_json)
       

        code = res["code_snippet"]["code"]
        
if code:
            
# Execute the code snippet
            exec(code)

copy


Conclusion

And that’s it! Now you’ve built a simple AI agent that can understand user queries and return Python code to perform actions like opening a website. You can extend this to do other things, such as closing apps or searching for something.

To get your API key and start building, head to Groq API Console. Have fun creating!

Powered by Froala Editor