-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
166 lines (153 loc) · 5.81 KB
/
Copy pathmain.py
File metadata and controls
166 lines (153 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
import asyncio
import random
import string
import json
from typing import Union, List, Optional
import time
app = FastAPI()
# 配置项
default_config = {
"first_token_delay": 0.5, # 首token返回时间(秒)
"stream_chunk_size_range": (5, 15), # 每次流式返回的字符数范围
"stream_interval_range": (0.1, 0.5), # 每次流式返回之间的时间间隔范围(秒)
"total_length_range": (30, 100), # 每次请求总的返回字符数范围
}
class ChatCompletionRequest(BaseModel):
model: str
messages: list
stream: bool = False
config: dict = None
@app.post("/v1/chat/completions")
async def chat_completions(request: Request, body: ChatCompletionRequest):
config = body.config or default_config
if not body.stream:
# 非流式直接返回假数据
return {
"id": "chatcmpl-fakeid",
"object": "chat.completion",
"created": 1234567890,
"model": body.model,
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "这是一个假回复。"},
"finish_reason": "stop"
}
],
"usage": {"prompt_tokens": 10, "completion_tokens": 10, "total_tokens": 20}
}
# 流式返回
async def fake_stream():
await asyncio.sleep(config["first_token_delay"])
total_length = random.randint(*config["total_length_range"])
sent = 0
while sent < total_length:
chunk_size = random.randint(*config["stream_chunk_size_range"])
interval = random.uniform(*config["stream_interval_range"])
chunk = ''.join(random.choices(string.ascii_letters + string.digits, k=chunk_size))
data = {
"id": "chatcmpl-fakeid",
"object": "chat.completion.chunk",
"created": 1234567890,
"model": body.model,
"choices": [
{
"delta": {"content": chunk},
"index": 0,
"finish_reason": None
}
]
}
yield f"data: {json.dumps(data, ensure_ascii=False)}\n\n"
sent += chunk_size
await asyncio.sleep(interval)
# 结束信号
yield "data: [DONE]\n\n"
return StreamingResponse(fake_stream(), media_type="text/event-stream")
class EmbeddingConfig(BaseModel):
embedding_length: int = 1536 # 默认长度
embedding_value: str = "random" # zeros/ones/random/custom
embedding_custom: Optional[List[float]] = None # embedding_value=custom时生效
response_delay: float = 0.0 # 响应延迟(秒)
class EmbeddingRequest(BaseModel):
model: str
input: Union[str, List[str]]
user: Optional[str] = None
config: Optional[EmbeddingConfig] = None
@app.post("/v1/embeddings")
async def embeddings(request: Request, body: EmbeddingRequest):
config = body.config or EmbeddingConfig()
# 延迟响应
if config.response_delay > 0:
await asyncio.sleep(config.response_delay)
# 处理 input
inputs = body.input if isinstance(body.input, list) else [body.input]
data = []
for idx, inp in enumerate(inputs):
if config.embedding_value == "zeros":
embedding = [0.0] * config.embedding_length
elif config.embedding_value == "ones":
embedding = [1.0] * config.embedding_length
elif config.embedding_value == "custom" and config.embedding_custom:
embedding = config.embedding_custom[:config.embedding_length]
if len(embedding) < config.embedding_length:
embedding += [0.0] * (config.embedding_length - len(embedding))
else: # random
embedding = [random.uniform(-1, 1) for _ in range(config.embedding_length)]
data.append({
"object": "embedding",
"index": idx,
"embedding": embedding,
})
return {
"object": "list",
"data": data,
"model": body.model,
"usage": {
"prompt_tokens": 0,
"total_tokens": 0
}
}
class ImageGenerationConfig(BaseModel):
base_url: str = "https://fake-image.example.com" # 基础URL
response_delay: float = 0.0 # 响应延迟(秒)
class ImageGenerationRequest(BaseModel):
prompt: str
model: Optional[str] = "dall-e-3"
n: Optional[int] = 1
quality: Optional[str] = "standard"
response_format: Optional[str] = "url"
size: Optional[str] = "1024x1024"
style: Optional[str] = "vivid"
user: Optional[str] = None
config: Optional[ImageGenerationConfig] = None
@app.post("/v1/images/generations")
async def image_generations(request: Request, body: ImageGenerationRequest):
config = body.config or ImageGenerationConfig()
# 延迟响应
if config.response_delay > 0:
await asyncio.sleep(config.response_delay)
# 生成假的图片URL
created = int(time.time())
data = []
for i in range(body.n):
# 生成随机ID作为图片URL的一部分
image_id = ''.join(random.choices(string.ascii_letters + string.digits, k=16))
url = f"{config.base_url}/images/{image_id}.png"
if body.response_format == "b64_json":
# 如果需要base64格式,返回一个假的base64字符串
data.append({
"b64_json": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
})
else:
# 默认返回URL格式
data.append({
"url": url
})
return {
"created": created,
"data": data
}