Coder Social home page Coder Social logo

airtai / faststream-gen Goto Github PK

View Code? Open in Web Editor NEW
37.0 4.0 6.0 14.28 MB

The faststream-gen library uses advanced AI to generate FastStream code from user descriptions, speeding up FastStream app development.

Home Page: https://faststream-gen.airt.ai

License: Apache License 2.0

Python 28.33% CSS 0.20% JavaScript 0.04% HTML 0.22% Jupyter Notebook 70.99% Shell 0.22%
fastkafka faststream faststream-gen

faststream-gen's Introduction

Code generator for FastStream

faststream-gen is a Python library that uses generative AI to automatically generate FastStream applications. Simply describe your application requirements, and faststream-gen will generate a production-grade FastStream project that is ready to deploy in no time.

PyPI PyPI - Downloads PyPI - Python Version GitHub Workflow Status GitHub


Documentation: https://faststream-gen.airt.ai

Source Code: https://github.com/airtai/faststream-gen


Getting Started

The code generator for FastStream is a Python library that automates the process of creating FastStream applications. It works by taking your application requirements and swiftly turning them into a ready-to-deploy FastStream application.

The key features are:

  • Automatic FastStream project generation: faststream-gen enables you to easily generate complete FastStream application with minimal effort. This library allows you to outline your application requirements, and it will quickly transform them into a fully-fledged FastStream project.
  • Tested code: faststream-gen provides dependable code through rigorous testing, including pre-implemented integration tests, ensuring stability and functionality, saving development time, and preventing common bugs.
  • Script Templates: Streamline the deployment of your FastStream application using faststream-gen’s built-in scripts, tailored for initiating, subscribing to Kafka topic and shutting down the local Kafka broker.
  • GitHub workflow files: faststream-gen integrates seamlessly with your version control and continuous integration pipeline through its GitHub workflow files. These predefined configuration files are optimized for FastStream projects, enabling smooth integration with GitHub Actions. You can automate tasks such as code validation, testing, and deployment, ensuring that your FastStream application remains in top shape throughout its development lifecycle.
faststream_gen_movie-2.mp4

Quick start

The following quick start guide will walk you through installing and configuring the faststream-gen library, demonstrating the creation of a new FastStream project in seconds.

Install

faststream-gen is published as a Python package and can be installed with pip:

pip install faststream-gen

If the installation was successful, you should now have the faststream-gen installed on your system. Run the below command from the terminal to see the full list of available commands:

faststream_gen --help
 Usage: faststream_gen [OPTIONS] [DESCRIPTION]                                  
                                                                                
 Effortlessly create a new FastStream project based on the app description.     
                                                                                
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│   description      [DESCRIPTION]  Summarize your FastStream application in a │
│                                   few sentences!                             │
│                                                                              │
│                                   Include details about messages, topics,    │
│                                   servers, and a brief overview of the       │
│                                   intended business logic.                   │
│                                                                              │
│                                   The simpler and more specific the app      │
│                                   description is, the better the generated   │
│                                   app will be. Please refer to the below     │
│                                   example for inspiration:                   │
│                                                                              │
│                                   Create a FastStream application using      │
│                                   localhost broker for testing and use the   │
│                                   default port number.  It should consume    │
│                                   messages from the "input_data" topic,      │
│                                   where each message is a JSON encoded       │
│                                   object containing a single attribute:      │
│                                   'data'.  For each consumed message, create │
│                                   a new message object and increment the     │
│                                   value of the data attribute by 1. Finally, │
│                                   send the modified message to the           │
│                                   'output_data' topic.                       │
│                                   [default: None]                            │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --input_file          -i      TEXT                   The path to the file    │
│                                                      with the app            │
│                                                      desription. This path   │
│                                                      should be relative to   │
│                                                      the current working     │
│                                                      directory.              │
│                                                      If the app description  │
│                                                      is passed via both a    │
│                                                      --input_file and a      │
│                                                      command line argument,  │
│                                                      the description from    │
│                                                      the command line will   │
│                                                      be used to create the   │
│                                                      application.            │
│                                                      [default: None]         │
│ --output_path         -o      TEXT                   The path to the output  │
│                                                      directory where the     │
│                                                      generated project files │
│                                                      will be saved. This     │
│                                                      path should be relative │
│                                                      to the current working  │
│                                                      directory.              │
│                                                      [default: .]            │
│ --model               -m      [gpt-3.5-turbo-16k|gp  The OpenAI model that   │
│                               t-4]                   will be used to create  │
│                                                      the FastStream project. │
│                                                      For better results, we  │
│                                                      recommend using         │
│                                                      'gpt-4'.                │
│                                                      [default:               │
│                                                      gpt-3.5-turbo-16k]      │
│ --verbose             -v                             Enable verbose logging  │
│                                                      by setting the logger   │
│                                                      level to INFO.          │
│ --dev                 -d                             Save the complete logs  │
│                                                      generated by            │
│                                                      faststream-gen inside   │
│                                                      the output_path         │
│                                                      directory.              │
│ --install-completion                                 Install completion for  │
│                                                      the current shell.      │
│ --show-completion                                    Show completion for the │
│                                                      current shell, to copy  │
│                                                      it or customize the     │
│                                                      installation.           │
│ --help                                               Show this message and   │
│                                                      exit.                   │
╰──────────────────────────────────────────────────────────────────────────────╯

Generate new project

The faststream-gen library uses OpenAI’s model to generate FastStream projects. In order to use the library, you’ll need to create an API key for OpenAI.

Once you have your API key, store it in the OPENAI_API_KEY environment variable. This is a necessary step for the library to work.

We’re now ready to create a new FastStream application with the faststream-gen library.

Simply run the following command to create a new FastStream application in the my-awesome-project directory:

faststream_gen "Create a FastStream application using localhost broker for testing and use the default port number. It should consume messages from the 'input_data' topic, where each message is a JSON encoded object containing a single attribute: 'data'. While consuming from the topic, increment the value of the data attribute by 1. Finally, send message to the 'output_data' topic." -o "./my-awesome-project"
✨  Generating a new FastStream application!
 ✔ Application description validated. 
 ✔ FastStream app skeleton code generated. akes around 15 to 45 seconds)...
 ✔ The app and the tests are generated.  around 30 to 90 seconds)...
 ✔ New FastStream project created. 
 ✔ Integration tests were successfully completed. 
 Tokens used: 9398
 Total Cost (USD): $0.02865
✨  All files were successfully generated!

Here’s a look at the directory hierarchy:

my-awesome-project
├── .github
│   └── workflows
│       ├── deploy_docs.yml
│       └── test.yml
├── .gitignore
├── LICENSE
├── README.md
├── app
│   ├── __init__.py
│   └── application.py
├── dev_requirements.txt
├── requirements.txt
├── scripts
│   ├── services.yml
│   ├── start_kafka_broker_locally.sh
│   ├── stop_kafka_broker_locally.sh
│   └── subscribe_to_kafka_broker_locally.sh
└── tests
    └── test_application.py

5 directories, 14 files

Let’s take a quick look at the generated application and test code.

application.py:

from faststream import FastStream, Logger
from faststream.kafka import KafkaBroker

broker = KafkaBroker("localhost:9092")
app = FastStream(broker)

to_output_data = broker.publisher("output_data")


@broker.subscriber("input_data")
async def on_input_data(msg: dict, logger: Logger) -> None:
    logger.info(f"{msg=}")
    incremented_data = msg["data"] + 1
    await to_output_data.publish({"data": incremented_data})

test_application.py:

import pytest

from faststream import Context
from faststream.kafka import TestKafkaBroker

from app.application import broker, on_input_data


@broker.subscriber("output_data")
async def on_output_data(msg: dict, key: bytes = Context("message.raw_message.key")):
    pass


@pytest.mark.asyncio
async def test_data_was_incremented():
    async with TestKafkaBroker(broker):
        await broker.publish({"data": 1}, "input_data")
        on_input_data.mock.assert_called_with({"data": 1})
        on_output_data.mock.assert_called_with({"data": 2})

Start localhost Kafka broker

In order for FastStream applications to publish and consume messages from the Kafka broker, it is necessary to have a running Kafka broker.

Along with application and test, faststream-gen also generated scripts directory. You can start local Kafka broker (inside docker container) by executing following commands:

cd my-awesome-project
# make all shell scripts executable
chmod +x scripts/*.sh
# start local kafka broker
./scripts/start_kafka_broker_locally.sh

Start application

To start the FastKafka application, run the following command:

faststream run  app.application:app

Stop application

To stop the FastKafka application, run the following command:

./scripts/stop_kafka_broker_locally.sh

Copyright

Copyright © 2023 onwards airt technologies ltd, Inc.

License

This project is licensed under the terms of the Apache License 2.0

faststream-gen's People

Contributors

davorrunje avatar harishmohanraj avatar rjambrecic avatar sternakt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

faststream-gen's Issues

Update the app_creation prompt to fix the error mentioned in the description

Error: PydanticUserError: regex is removed. use pattern instead

Description: The model uses old syntax of pydantic for declaring the field types.

Fix: Instruct the model to use the latest syntax while creating the Field instance in the prompt. Refer to the below example

Current implementation: currency: str = Field(..., description="Currency of the product.", regex="^[A-Z]{3}$")
Expected implementation: currency: str = Field(..., description="Currency of the product.", pattern="^[A-Z]{3}$")

At times, the generation and validation of the asyncapi.yml fails, even after multiple retries.

Please run the below app description multiple times to replicate the issue

App description:

Create a FastKafka application using localhost broker for testing, staging.airt.ai for staging and prod.airt.ai for production. Use the default port number.

It should consume messages from 'receive_name' topic and the message will be a JSON encoded object with only one attribute: user_name.

For each consumed message, construct a new message object and append 'Hello ' in front of the name attribute. Finally, publish the consumed message to 'send_greetings' topic.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.