DEV Community

Akshay Keerthi
Akshay Keerthi

Posted on

Building a Natural Language to Java Generator using Lyzr Agent-Api

In today’s rapidly evolving tech landscape, developers are always on the lookout for tools that can simplify and speed up their coding process.

The Java Generator is one such innovative application that leverages the power of natural language processing, combined with the Lyzr Agent-API, to convert everyday language into Java code. Whether you’re a beginner or an expert, this tool makes coding more accessible than ever.

Image description

The Java Generator is designed to take user input in plain English and output accurate Java code. This application is built using Streamlit for the user interface and Lyzr Agent-API for natural language processing and code generation. Let’s walk through how this application works and explore the key components that make it tick.

Lets get started : Step-by-Step Explanation
Setting Up the Application

The application starts by importing the necessary libraries and loading the API keys from environment variables. Here’s the relevant snippet:

import os
from lyzr_agent import LyzrAgent
import streamlit as st
from dotenv import load_dotenv

load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
LYZR_API_KEY = os.getenv("LYZR_API_KEY")
Enter fullscreen mode Exit fullscreen mode

This setup ensures that your API keys are securely stored and easily accessible when needed.

Initializing the LyzrAgent

The core functionality of the app lies in the LyzrAgent class, which interacts with the Lyzr Agent-API. The agent is initialized with the API keys and then configured to create a new environment for generating Java code based on natural language input.


Agent = LyzrAgent(
    api_key=LYZR_API_KEY,
    llm_api_key=OPENAI_API_KEY
)
Enter fullscreen mode Exit fullscreen mode

This initialization is crucial as it sets the stage for the interaction between user inputs and the Lyzr API.

Creating the Agent and Environment

The create_agent function is where the magic happens. This function sets up an environment with specific features and tools, such as perplexity_search, which is used to find and synthesize relevant Java code based on the user’s input.

@st.cache_resource
def create_agent():
    env_id = Agent.create_environment(
        name="Post_java",
        features=[{
            "type": "TOOL_CALLING",
            "config": {"max_tries": 3},
            "priority": 0
        }],
        tools=["perplexity_search"]
    )

    agent_id = Agent.create_agent(
        env_id=env_id['env_id'],
        system_prompt=prompt,
        name="shell"
    )
Enter fullscreen mode Exit fullscreen mode

This function sets up the environment where the agent will operate, including the tools it will use to generate the Java code.

User Input and Code Generation

The user interface allows the user to input their desired functionality in plain English. When the “Generate!” button is clicked, the input is sent to the Lyzr agent, which processes the input and returns the corresponding Java code.

query = st.text_area("Give your input in natural language below.")

if st.button("Generate!"):
    agent = create_agent()
    chat = Agent.send_message(
        agent_id=agent['agent_id'],
        user_id="default_user",
        session_id="akshay@lyzr.ai",
        message=query
    )

    st.markdown(chat['response'])
Enter fullscreen mode Exit fullscreen mode

This code handles the interaction between the user and the agent, ensuring that the natural language input is accurately converted into Java code.

The Java Generator application demonstrates the incredible potential of natural language processing when combined with powerful APIs like Lyzr Agent-API.

By breaking down the barriers between natural language and code, this tool makes coding more intuitive and accessible for developers of all skill levels. Whether you’re looking to quickly prototype an idea or generate production-ready code, the Java Generator has you covered.

App link: https://lyzr-java.streamlit.app/

Source Code: https://github.com/isakshay007/java

For any inquiries or support, feel free to contact Lyzr. You can learn more about Lyzr and their offerings through the following links:

Website: Lyzr.ai
Book a Demo: Book a Demo
Discord: Join our Discord community
Slack: Join our Slack channel

Top comments (0)