简单的chatGPT 网页界面

 

        大语言模型的应用大多数采用python 语言实现,为了与其它应用结合,最简单的方法就是采样网页 RESTful API 服务。目前流行的Python 的Web 服务是FastAPI。本文记录了一个简单LLM 对话FastAPI 的网站服务的实验过程。

界面

安装

安装如下两个主要模块

pip install fastapi 
pip install uvicorn

文件目录结构

FastAPI 具有固定的目录结构,Templates 中包含了index.hml

static 文件夹

,static 中包含了js,css,images等文件夹。

主程序(main.py)

import asyncio
import nest_asyncio
from langchain_core.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.chat_models import ErnieBotChat
from pydantic import BaseModel
nest_asyncio.apply()
llm= ErnieBotChat(model_name='ERNIE-Bot', #ERNIE-Bot
                    ernie_client_id='xxxxxxxx',
                    ernie_client_secret='xxxxxxxxxxx',
                    temperature=0.75,
                    )
template = """You are a nice chatbot having a conversation with a human.
New human question: {question}
Response:"""
prompt = PromptTemplate.from_template(template)
# Notice that we need to align the `memory_key`
conversation = LLMChain(
    llm=llm,
    prompt=prompt,
    verbose=True,
)
class Prompt(BaseModel):
    Method: str
    Message:str  
    
    
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get("/")
async def root(request: Request):
   # return {"message": "Hello, World!"}
 
   return templates.TemplateResponse("index.html",{
            "request": request
        })
@app.post("/generate/")
def generate(prompt:Prompt):
    print(prompt)
    AIresponse=conversation.predict(question=prompt.Message)
    response=prompt
    response.Message=AIresponse
    print(response)
    return {"response": response}
async def run_server():
    uvicorn.run(app, host="localhost", port=8000)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值