Agentic Data Model Generation and Structured Output Powered by CAMEL & Qwen
You can also check this cookbook in colab here (Use the colab share link)
⭐ Star us on GitHub, join our Discord, or follow us on XThis notebook demonstrates how to set up and leverage CAMEL’s ability of structure output, like JSON, and Pydantic objects.In this notebook, you’ll explore:
CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.
Structure output: The ability of LLMs to return structured output.
Qwen: The Qwen model is a series of LLMs and multimodal models developed by the Qwen Team at Alibaba Group. Designed for diverse scenarios, Qwen integrates advanced AI capabilities, such as natural language understanding, text and vision processing, programming assistance, and dialogue simulation.
This setup not only demonstrates a practical application but also serves as a flexible framework that can be adapted for various scenarios requiring structure output and data generation.
# Prompt for the API key securelyimport osfrom getpass import getpassqwen_api_key = getpass('Enter your API key: ')os.environ["QWEN_API_KEY"] = qwen_api_key
Alternatively, if running on Colab, you could save your API keys and tokens as Colab Secrets, and use them across notebooks.To do so, comment out the above manual API key prompt code block(s), and uncomment the following codeblock.⚠️ Don’t forget granting access to the API key you would be using to the current notebook.
Copy
# import os# from google.colab import userdata# os.environ["QWEN_API_KEY"] = userdata.get("QWEN_API_KEY")
In this section, we’ll demonstrate how to Qwen to generate structured data. Qwen is a good example in Camel of using prompt engineering for structure output. It offers powerful models like Qwen-max, Qwen-coder, but yet not support structure output by itself. We can then make use of its own ability to generate structured data.Import necessary libraries, define the Qwen agent, and define the Pydantic classes.The following function retrieves relevant information from a list of URLs based on a given query. It combines web scraping with Firecrawl and CAMEL’s AutoRetriever for a seamless information retrieval process. (Some explanation)
First, let’s try if we don’t specific format just in prompt.
Copy
assistant_sys_msg = BaseMessage.make_assistant_message( role_name="Assistant", content="You are a helpful assistant in helping user to generate necessary data information.",)user_msg = """Help me 1 student info in JSON format, with the following format:{ "name": "string", "age": "string", "email": "string"}"""response = qwen_agent.step(user_msg)print(response.msgs[0].content)
It did it, but we need to expand our prompts, and the result still has some annoying extra texts, and we still need to parse it into valid JSON object by ourselves.A more elegant way is to use the response_format argument in .step() function:
Copy
qwen_agent.reset()user_msg = "Help me 1 student info in JSON format"response = qwen_agent.step(user_msg, response_format=Student)print(response.msgs[0].content)
And we can directly extract the Pydantic object in response.msgs[0].parsed field:
Hooray, now we successfully generate 1 entry of student, suppose we want to generate more, we can still achieve this easily.
Copy
class StudentList(BaseModel): studentList: list[Student]user_msg = "Help me 5 random student info in JSON format"response = qwen_agent.step(user_msg, response_format=StudentList)print(response.msgs[0].content)print(response.msgs[0].parsed)
That’s it! We just generate 5 random students out of nowhere by using Qwen Camel agent!
This notebook has guided you through setting up and running Qwen chat agent and use it to generate structured data.Key tools utilized in this notebook include:
CAMEL: A powerful multi-agent framework that enables Retrieval-Augmented Generation and multi-agent role-playing scenarios, allowing for sophisticated AI-driven tasks.
Qwen data generation: Use Qwen model to generate structured data for further use of other applications.
That’s everything: Got questions about 🐫 CAMEL-AI? Join us on Discord! Whether you want to share feedback, explore the latest in multi-agent systems, get support, or connect with others on exciting projects, we’d love to have you in the community! 🤝Check out some of our other work: