dirtyhand-tester 开心一刻: 使用 steamship 构建第一个 AI 应用

simonpatrick · 2023年04月24日 · 4914 次阅读

使用 steamship 构建第一个 AI 应用,这个应用其实就是根据表结构生成不同类型的实体类。

先说实现的结果是什么?

  • 输入语言: python
  • 输入 entity 框架名称: pydantic
  • 输入 references: 建表的 SQL 或者 JSON 文件

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
  1. 设置 api-key, 文件在~/.steamship.json json { "apiKey": "mykey" }
  2. 创建 python 项目,使用 pip 安装 streamship shell pip install streamship
  3. 创建第一个可以被外部调用的 API, 文件为 api.py ```python

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 程序就完成了, 并且是接口可以访问的.

  1. 如果想用这个外部接口 steamship 也提供了非常方便的调用方式
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  
)

小结

  1. 这个程序很简单,不过 steamship 这个工具还不错,接口和部署都非常方便
  2. steamship 这个写本地函数,部署之后就暴露为 api 的方式不错,客户端调用方式也很简单,值的学习
  3. steamship 上面还有不少其他内容,都是刚刚开始,值的看看

参考阅读:

其实没有太多内容,开心就好

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暫無回覆。
需要 登录 後方可回應,如果你還沒有帳號按這裡 注册