What is this AI application used for?#
This application is actually used to generate different types of entity classes based on table structures.
What are the implementation results?
- Input language: python
- Input entity framework name: pydantic
- Input references: SQL or JSON files for table creation
By running it, you can get the generated python code.
Below is the Python code for a Pydantic model based on the given SQL table:
from typing import Optional
from pydantic import BaseModel
from datetime import datetime
class EDict(BaseModel):
id: int
create_by: Optional[str]
create_time: Optional[datetime]
update_by: Optional[str]
update_time: Optional[datetime]
code: str
name: Optional[str]
remark: Optional[str]
class Config:
orm_mode = True
Please note that the Pydantic model is a simple data validation and serialization library for Python and it doesn't interact with databases directly. You will need an additional library such as SQLAlchemy for working with databases or implement your code to interact with your database. Additionally, if you are using a newer version of Pydantic, you may need to use the Field
method for unique constraint and other validation options.
It also supports JPA for JAVA and entity definition for Typescript. You just need to input the desired language and package name.
How to implement this backend AI interface?#
This is a backend API, and it is very easy to implement using Steamship:
- Register an account at https://www.steamship.com/ and obtain an API key.
- Install Steamship:
pip install steamship
- Set the API key in the ~/.steamship.json file:
{
"apiKey": "mykey"
}
- Create a Python project and install Steamship using pip:
pip install steamship
- Create the first API that can be called externally. Create a file named api.py:
from steamship import check_environment, RuntimeEnvironments, Steamship
from steamship.invocable import post, PackageService
class EntityGeneratePackage(PackageService):
PROMPT = "根据以下内容:{references},生成{language}基于{lib_name}实体类"
@post("generate")
def generate(self, language: str, lib_name: str, references: str):
gpt4 = self.client.use_plugin("gpt-4")
task = gpt4.generate(text=self.PROMPT.format(language=language, lib_name=lib_name, references=references))
task.wait()
return task.output.blocks[0].text
- Deploy:
ship deploy
That's it! The first AI program using OpenAI GPT-4 is completed and accessible through the API.
- If you want to use this external API, Steamship provides a convenient way to call it:
from steamship import Steamship
# Load the package instance stub.
pkg = Steamship.use(
"entity-generator",
api_key="YOUR_API_KEY"
)
# Invoke the method
resp = pkg.invoke(
"generate",
language=VALUE,
lib_name=VALUE,
references=VALUE
)
Summary#
- This program is simple, but Steamship is a good tool that makes API interface and deployment very convenient.
- Steamship's approach of writing local functions and exposing them as APIs after deployment is great, and the client calling method is also simple. It's worth learning.
- Steamship has many other features worth exploring, and it's just the beginning.
- In the AI era, countless small interfaces can be connected to achieve good functionality.
- Although code generation like this can be implemented with code, it is obvious that using AI will be much faster. So developers who despise no-code/low-code may need to be careful, and those who don't write code will benefit greatly from AI, as they can use natural language to get code assistance and automate some work without needing to know too much.