Building a Diet Assistant using Lyzr SDK
Tags: ai, programming, diet, python
Published: 2024-06-09 11:35:32

In today’s fast-paced world, maintaining a balanced diet can be challenging. However, with the **Diet Assistant** powered by **Lyzr SDK**, you can now effortlessly track and optimize your nutrition. This blog post will walk you through the incredible features of the Diet Assistant and how it can transform your dietary habits for the better.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6gax4fwdny9szugbuqn.png)

The **Diet Assistant** is an advanced application designed to help you monitor and analyze your daily food intake. Leveraging the power of **Lyzr Automata** Agent and OpenAI’s state-of-the-art models, this tool provides personalized nutritional insights that are both practical and actionable. Whether your goal is to enhance your overall health, achieve fitness targets, or meet specific dietary requirements, the Diet Assistant is your perfect companion.

**Why use Lyzr SDK’s?**

With **Lyzr SDKs** , crafting your own **GenAI** application is a breeze, requiring only a few lines of code to get up and running swiftly.

[Checkout the Lyzr SDK’s](https://docs.lyzr.ai/homepage)

**Lets get Started!**

Create an **app.py** file

```
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent, Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from PIL import Image
from lyzr_automata.tasks.task_literals import InputType, OutputType
import os
```

This code creates a **Streamlit** app using the Lyzr Automata SDK and OpenAI’s language model to build a diet assistant tool. It imports necessary libraries for the web interface, AI model, and image handling. The OpenAI API key is set from Streamlit secrets. The app hides the default header, displays a logo, and provides a text input for users to log their food intake. An OpenAIModel instance is configured with specific parameters. The generation function defines an AI agent to analyze the user's input and provide nutritional insights.

```
# Set the OpenAI API key
os.environ["OPENAI_API_KEY"] = st.secrets["apikey"]
```
This line sets the **OpenAI API key** as an environment variable, retrieving it securely from Streamlit’s secrets for authentication.

```
input = st.text_input("Please enter your daily food intake:",placeholder=f"""Type here""")
```
This line creates a text input field in a **Streamlit** app where users can enter their daily food intake. It includes a label and a placeholder text to guide the user.

```
open_ai_text_completion_model = OpenAIModel(
    api_key=st.secrets["apikey"],
    parameters={
        "model": "gpt-4-turbo-preview",
        "temperature": 0.2,
        "max_tokens": 1500,
    },
)
```
This code instantiates an **OpenAIModel** object from the lyzr_automata package, configuring it to use the **GPT-4** language model with specific parameters. It securely retrieves the API key from Streamlit's secrets for authentication.

```
def generation(input):
    generator_agent = Agent(
        role=" Expert DIETITIAN ",
        prompt_persona=f"Your task is to EMPOWER users to TRACK and ANALYZE their DAILY FOOD INTAKE.")
    prompt = f"""
[Prompts here]
 """
```
This function generation defines an AI agent using the Agent class from the Lyzr Automata package. The agent is assigned the role of an "**Expert Dietitian**" with a specified prompt persona. The prompt persona instructs the agent on its task, which is to empower users to track and analyze their daily food intake.

```
   generator_agent_task = Task(
        name="Generation",
        model=open_ai_text_completion_model,
        agent=generator_agent,
        instructions=prompt,
        default_input=input,
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
    ).execute()

    return generator_agent_task 
```
This code snippet creates a task instance named “Generation” using the Lyzr Automata package’s Task class. It specifies parameters such as the model (open_ai_text_completion_model), agent (generator_agent), instructions (the prompt defined earlier), default input (user input), and output/input types. The **execute()** method is called to execute the task, and the result is returned.

```
if st.button("Insights"):
    solution = generation(input)
    st.markdown(solution)
```
This piece of code creates a button using Streamlit’s **st.button()** function. When the button labeled "Insights" is clicked, it triggers the **generation()** function, passing the user input (input) as an argument. The result of the function execution is stored in the solution variable, which is then displayed using Streamlit's **st.markdown()** function.

**App link**: https://dietassistant-lyzr.streamlit.app/

**Source Code**: https://github.com/isakshay007/Diet_Assistant

The **Diet Assistant** is powered by the Lyzr Automata Agent, utilizing the capabilities of OpenAI’s GPT-4 Turbo. For any inquiries or issues, please contact Lyzr. You can learn more about Lyzr and their offerings through the following links:

**Website**: [Lyzr.ai](https://www.lyzr.ai/)
**Book a Demo**: [Book a Demo](https://www.lyzr.ai/book-demo/)
**Discord**: [Join our Discord community](https://discord.com/invite/nm7zSyEFA2)
**Slack**: [Join our Slack channel](https://anybodycanai.slack.com/join/shared_invite/zt-2a7fr38f7-_QDOY1W1WSlSiYNAEncLGw#/shared-invite/email)