mirror of
https://github.com/labring/FastGPT.git
synced 2025-12-20 01:09:24 +08:00
Create Python API (#457)
* 更新镜像 * 更新镜像信息 * 更新镜像信息 * Create openai_api.py * Create requirements.txt * Create README.md * 添加python接口 * Delete python directory * Create README.md * Create Python API * 文件结构化 * 文件结构化
This commit is contained in:
25
python/api/test/fetch_test.py
Normal file
25
python/api/test/fetch_test.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import requests
|
||||
|
||||
# 接口的URL
|
||||
api_url = "http://127.0.0.1:6010/generate_summary/"
|
||||
|
||||
# 请求的数据
|
||||
data = {
|
||||
"url": "https://bing.com",
|
||||
"level": 1
|
||||
}
|
||||
|
||||
# 发送POST请求
|
||||
response = requests.post(api_url, json=data)
|
||||
|
||||
# 检查响应状态
|
||||
if response.status_code == 200:
|
||||
# 请求成功,打印结果
|
||||
summaries = response.json()
|
||||
for summary in summaries:
|
||||
print(f"URL: {summary['url']}")
|
||||
print(f"Title: {summary['title']}")
|
||||
print(f"Summary: {summary['summary']}\n")
|
||||
else:
|
||||
# 请求失败,打印错误信息
|
||||
print(f"Failed to generate summary with status code {response.status_code}: {response.text}")
|
||||
49
python/api/test/office_test.py
Normal file
49
python/api/test/office_test.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import requests
|
||||
import pytest
|
||||
from docx import Document
|
||||
import os
|
||||
from tempfile import NamedTemporaryFile
|
||||
from docx.shared import Inches
|
||||
|
||||
image_path = os.path.join(os.path.dirname(__file__), "test.png")
|
||||
|
||||
# 定义一个函数来创建一个新的Word文档,并添加一个图片
|
||||
def create_test_docx_with_image():
|
||||
# 使用临时文件来避免文件名冲突
|
||||
temp_file = NamedTemporaryFile(delete=False, suffix='.docx')
|
||||
# 创建一个文档对象
|
||||
doc = Document()
|
||||
# 添加一个段落
|
||||
doc.add_paragraph("This is a test document with an image.")
|
||||
# 添加一个图片,确保提供的图片路径是有效的
|
||||
doc.add_picture(image_path, width=Inches(1.25)) # 图片宽度设为1.25英寸
|
||||
# 保存文档到临时文件
|
||||
doc.save(temp_file.name)
|
||||
# 关闭临时文件
|
||||
temp_file.close()
|
||||
# 返回文件路径
|
||||
return temp_file.name
|
||||
|
||||
# 定义一个函数,它将创建并发送多个Word文档,并返回响应对象列表
|
||||
def get_responses():
|
||||
responses = []
|
||||
# 创建并发送10个文档
|
||||
for _ in range(10):
|
||||
test_file_path = create_test_docx_with_image()
|
||||
with open(test_file_path, "rb") as f:
|
||||
files = {"file": (os.path.basename(test_file_path), f, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")}
|
||||
response = requests.post("http://127.0.0.1:6010/extract_text/", files=files)
|
||||
responses.append(response)
|
||||
# 测试完成后删除文件
|
||||
os.unlink(test_file_path)
|
||||
return responses
|
||||
|
||||
# 使用pytest的parametrize装饰器测试所有响应
|
||||
@pytest.mark.parametrize("response", get_responses())
|
||||
def test_response(response):
|
||||
# 断言响应的状态码为200
|
||||
assert response.status_code == 200
|
||||
# 断言响应的内容类型是application/json
|
||||
assert "application/json" in response.headers["Content-Type"]
|
||||
# 断言响应的数据包含文本信息
|
||||
assert "text" in response.json()
|
||||
Reference in New Issue
Block a user