Python - FastAPI implements get and post requests

Table of contents

I. Introduction

2. FastAPI Server construction

1.get - read_items

2.post - create_item

3.uvicorn - run_app

3. Postman request

1.post - create_item

2.get - read_items

4. Requests

1.post - create_item

2.get - read_items

5. Summary


I. Introduction

The relevant knowledge of LLM was introduced earlier. From sample loading and model loading to subsequent model training and model inference, we have experienced the complete LLM LoRA fine-tuning and inference process. Based on the previous pre-training model, we try to use FastAPI to build interface services. This article mainly introduces the most basic FastAPI get and post usage, and then introduces the complete server deployment.

2. FastAPI Server construction

Next we build an API service, which contains two methods:

read_items  - print out the elements of the items array

create_item  - adds an element to the items array

1.get - read_items

The get method is a type of HTTP request, usually used to obtain data from the server. Since the read_items method obtains the items array from the server, it corresponds to the get request. Get requests usually do not have a request body, and all parameters are placed in the query string of the URL, so there are certain security issues.

# -*- coding: utf-8 -*-
from fastapi import FastAPI, Form

app = FastAPI()

# 示例数据
items = []


# GET 请求
@app.get("/items")
async def read_items():
    return items

2.post - create_item

The post method is another HTTP request type different from get, and is usually used to send data to the server. Post usually contains a request body, and the transmitted parameters do not need to be explicitly reflected in the requested URL through '?', but can be passed through a JSON object, which is safer than the get method. Compared with get, the post operation is not idempotent and may obtain different results each time it is executed. The get operation is generally considered idempotent, which means that the results obtained by executing get multiple times should be the same.

# POST 请求
@app.post("/items")
async def create_item(item: str = Form(None)):
    items.append(item)
    return {"message": "Item created successfully: {}".format(str(item))}

Here, after the Post request is successful, there will be a success prompt and the successfully added items will be printed.

3.uvicorn - run_app

The uvicorn.run(app, host="192.XXX.XXX", port=8001) method is the command line interface of uvicorn. Among them, app is the application object you want to run, which corresponds to the get and post methods of FastAPI above. Host and port are the IP address and port that the service will listen on. This method will start an ASGI server to provide services for your application at the specified host and port. The URL of subsequent HTTP requests is also based on the above host and port. Next we start a FastAPI service on this machine.

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, host='0.0.0.0', port=7788)

After running the main function, the following prompt appears, which means that the Server has started successfully. Our subsequent corresponding HTTP request URL is the blue link in the figure http://0.0.0.0:7788: 

3. Postman request

For the HTTP service built above, we can make get and post requests through the displayed App such as postman.

1.post - create_item

Open postman, select the POST command, enter the corresponding URL + the path corresponding to the function. The paths corresponding to the get and post requests above are URL + items. Pass the item to be added at the corresponding form-data. Note that the key here must be the same as the function definition. The parameter names must correspond exactly, otherwise an error will be reported. Execute the send button to send the Http request. From the returned Body, we see that python is successfully added to the items list. Later we modify the value to 'java' and 'c++' and send the post instructions in sequence.

The server side also requires specific information for each request. status_code == 200 means the request is successful.

2.get - read_items

Switch the request mode to GET. Since the get command does not pass parameters, you can directly enter the corresponding address Send to send the request. The returned result contains the three elements we added above: python, java, c++. The server side also adds the corresponding get request log:

4. Requests

Above, Postman uses an explicit page method to send HTTP requests to the server and obtain the results. A more conventional approach is to use the requests library to use code to send requests and process the returned results.

1.post - create_item

def post(text):
    url = 'http://0.0.0.0:7788/items'

    # 构建请求数据
    data = {
        'item': text
    }

    # 发送 post 请求
    response = requests.post(url=url, data=data, timeout=10)

    # 检查响应状态
    if response.status_code == 200:
        print('请求成功')
        print(response.json())
    else:
        print('请求失败,状态码:', response.status_code)

requests.post adds the corresponding url and data. The data is in the form of a map, which contains the key corresponding to the post function. At the end, status_code will be used to determine whether the request is successful. It can be seen from the server log that create_item was also executed successfully this time:

2.get - read_items

def get():
    url = 'http://0.0.0.0:7788/items'
    response = requests.get(url)

    if response.status_code == 200:
        data = response.json()
        print('成功获取,返回:', data)
    else:
        print('请求失败,状态码:', response.status_code)

requests.get can directly pass in the corresponding URL to obtain the corresponding information:

The following log on the server side represents the success of this get request: 

5. Summary

The above describes how to use FastAPI to build a simple Server Demo, and then build the LLM Server based on this method.

Guess you like

Origin blog.csdn.net/BIT_666/article/details/133016549