使用 steamship 构建第一个 AI 应用,这个应用其实就是根据表结构生成不同类型的实体类。
先说实现的结果是什么?
run: 就可以得到生成好的 python 代码
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.
同时也支持JAVA的JPA,Typescript的entity定义,只要输入自己想要的语言和包名就可以.
这是一个后端的API,使用streamship实现实际上非常简单:
1. https://www.steamship.com/ 注册账号,并且获得api-key
2. 安装streamship
```shell
pip install steamship
json
{
"apiKey": "mykey"
}
shell
pip install streamship
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
6. 部署
```shell
ship deploy
就这样子第一个调用 openai gpt4 的 AI 程序就完成了, 并且是接口可以访问的.
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
)
参考阅读:
其实没有太多内容,开心就好