111
Browse files- routers/__pycache__/v1.cpython-311.pyc +0 -0
- routers/v1.py +1 -0
- 程序开发笔记.md +232 -0
routers/__pycache__/v1.cpython-311.pyc
CHANGED
|
Binary files a/routers/__pycache__/v1.cpython-311.pyc and b/routers/__pycache__/v1.cpython-311.pyc differ
|
|
|
routers/v1.py
CHANGED
|
@@ -59,6 +59,7 @@ async def chat_completions(chat_request: ChatCompletionRequest, request: Request
|
|
| 59 |
headers = {"Content-Type": "application/json"}
|
| 60 |
if auth_header:
|
| 61 |
headers["Authorization"] = auth_header
|
|
|
|
| 62 |
|
| 63 |
print('\n\n\n将要发生的头信息')
|
| 64 |
print(headers)
|
|
|
|
| 59 |
headers = {"Content-Type": "application/json"}
|
| 60 |
if auth_header:
|
| 61 |
headers["Authorization"] = auth_header
|
| 62 |
+
headers["Access-Control-Allow-Origin"]= "*"
|
| 63 |
|
| 64 |
print('\n\n\n将要发生的头信息')
|
| 65 |
print(headers)
|
程序开发笔记.md
CHANGED
|
@@ -2,3 +2,235 @@
|
|
| 2 |
|
| 3 |
1. 主文件为 app.py
|
| 4 |
2. 以版本作为路由路径,将版本对应的路由放到 routers 文件夹下,routers/v1.py、routers/v2.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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作为路由路径,将版本对应的路由放到 routers 文件夹下,routers/v1.py、routers/v2.py
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
## js 实现的proxy分析:
|
| 8 |
+
|
| 9 |
+
**基本参数**
|
| 10 |
+
const BASE_URL = "https://generativelanguage.googleapis.com";
|
| 11 |
+
const API_VERSION = "v1beta";
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
使用postman
|
| 15 |
+
**收到的heads信息为:**
|
| 16 |
+
```json
|
| 17 |
+
{
|
| 18 |
+
"accept": "*/*",
|
| 19 |
+
"accept-encoding": "gzip, br",
|
| 20 |
+
"authorization": "Bearer AIzaSyAjfpEhPQGilY6z98vSV2eTxFkTWKMtzB0",
|
| 21 |
+
"cf-connecting-ip": "207.244.245.191",
|
| 22 |
+
"cf-ipcountry": "US",
|
| 23 |
+
"cf-ray": "9159add17d31f155",
|
| 24 |
+
"cf-visitor": "{\"scheme\":\"https\"}",
|
| 25 |
+
"connection": "Keep-Alive",
|
| 26 |
+
"content-length": "211",
|
| 27 |
+
"content-type": "application/json",
|
| 28 |
+
"host": "gmn1.ainet.ltd",
|
| 29 |
+
"postman-token": "56134639-0283-484e-a3a6-5c56ed803f88",
|
| 30 |
+
"user-agent": "PostmanRuntime/7.43.0",
|
| 31 |
+
"x-forwarded-proto": "https",
|
| 32 |
+
"x-real-ip": "207.244.245.191"
|
| 33 |
+
}
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
**收到的body信息为:**
|
| 37 |
+
也就是 await request.json()
|
| 38 |
+
```json
|
| 39 |
+
{
|
| 40 |
+
"model": "gemini-2.0-flash-exp",
|
| 41 |
+
"messages": [
|
| 42 |
+
{
|
| 43 |
+
"role": "system",
|
| 44 |
+
"content": "你是一个有用的助手"
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"role": "user",
|
| 48 |
+
"content": "你好!请介绍一下自己。"
|
| 49 |
+
}
|
| 50 |
+
]
|
| 51 |
+
}
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
**准备用来向后端发送的headers信息为:**
|
| 55 |
+
```json
|
| 56 |
+
{
|
| 57 |
+
"x-goog-api-client": "genai-js/0.21.0",
|
| 58 |
+
"x-goog-api-key": "your_api_key",
|
| 59 |
+
"Content-Type": "application/json"
|
| 60 |
+
}
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
**准备用来向后端发送的body信息为:**
|
| 64 |
+
已经通过 transformRequest 函数做了转换,转换后的数据为:
|
| 65 |
+
```json
|
| 66 |
+
{
|
| 67 |
+
"system_instruction": {
|
| 68 |
+
"parts": [
|
| 69 |
+
{
|
| 70 |
+
"text": "你是一个有用的助手"
|
| 71 |
+
}
|
| 72 |
+
]
|
| 73 |
+
},
|
| 74 |
+
"contents": [
|
| 75 |
+
{
|
| 76 |
+
"role": "user",
|
| 77 |
+
"parts": [
|
| 78 |
+
{
|
| 79 |
+
"text": "你好!请介绍一下自己。"
|
| 80 |
+
}
|
| 81 |
+
]
|
| 82 |
+
}
|
| 83 |
+
],
|
| 84 |
+
"safetySettings": [
|
| 85 |
+
{
|
| 86 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 87 |
+
"threshold": "BLOCK_NONE"
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 91 |
+
"threshold": "BLOCK_NONE"
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 95 |
+
"threshold": "BLOCK_NONE"
|
| 96 |
+
},
|
| 97 |
+
{
|
| 98 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 99 |
+
"threshold": "BLOCK_NONE"
|
| 100 |
+
},
|
| 101 |
+
{
|
| 102 |
+
"category": "HARM_CATEGORY_CIVIC_INTEGRITY",
|
| 103 |
+
"threshold": "BLOCK_NONE"
|
| 104 |
+
}
|
| 105 |
+
],
|
| 106 |
+
"generationConfig": {}
|
| 107 |
+
}
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
**准备用来向后端发送的url为:**
|
| 111 |
+
```
|
| 112 |
+
https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
**后端返回的headers信息为:**
|
| 116 |
+
|
| 117 |
+
```json
|
| 118 |
+
{
|
| 119 |
+
"alt-svc": "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000",
|
| 120 |
+
"cf-cache-status": "DYNAMIC",
|
| 121 |
+
"cf-ray": "915a65d4b121df2f-SEA",
|
| 122 |
+
"connection": "keep-alive",
|
| 123 |
+
"content-type": "application/json; charset=UTF-8",
|
| 124 |
+
"date": "Fri, 21 Feb 2025 23:08:21 GMT",
|
| 125 |
+
"server": "cloudflare",
|
| 126 |
+
"server-timing": "gfet4t7; dur=1041",
|
| 127 |
+
"transfer-encoding": "chunked",
|
| 128 |
+
"vary": "Origin, X-Origin, Referer, accept-encoding",
|
| 129 |
+
"x-content-type-options": "nosniff",
|
| 130 |
+
"x-frame-options": "SAMEORIGIN",
|
| 131 |
+
"x-xss-protection": "0"
|
| 132 |
+
}
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
**跨域处理后的headers信息为:**
|
| 136 |
+
```json
|
| 137 |
+
{
|
| 138 |
+
"alt-svc": "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000",
|
| 139 |
+
"cf-cache-status": "DYNAMIC",
|
| 140 |
+
"cf-ray": "915a70414118757f-SEA",
|
| 141 |
+
"connection": "keep-alive",
|
| 142 |
+
"content-type": "application/json; charset=UTF-8",
|
| 143 |
+
"date": "Fri, 21 Feb 2025 23:15:28 GMT",
|
| 144 |
+
"server": "cloudflare",
|
| 145 |
+
"server-timing": "gfet4t7; dur=1237",
|
| 146 |
+
"transfer-encoding": "chunked",
|
| 147 |
+
"vary": "Origin, X-Origin, Referer, accept-encoding",
|
| 148 |
+
"x-content-type-options": "nosniff",
|
| 149 |
+
"x-frame-options": "SAMEORIGIN",
|
| 150 |
+
"x-xss-protection": "0"
|
| 151 |
+
}
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
**后端返回的body信息为:**
|
| 155 |
+
```json
|
| 156 |
+
{
|
| 157 |
+
"candidates": [
|
| 158 |
+
{
|
| 159 |
+
"content": {
|
| 160 |
+
"parts": [
|
| 161 |
+
{
|
| 162 |
+
"text": "你好!我是一个大型语言模型,由 Google 训练。我的主要功能是处理和生成文本,我可以回答你的问题、提供信息、翻译语言、创作不同类型的文本内容,以及执行其他各种任务。我的知识库庞大且持续更新,但我无法访问互联网进行实时搜索。你可以把我理解成一个知识渊博且善于表达的助手。\n\n请告诉我你有什么需要我帮忙的?\n"
|
| 163 |
+
}
|
| 164 |
+
],
|
| 165 |
+
"role": "model"
|
| 166 |
+
},
|
| 167 |
+
"finishReason": "STOP",
|
| 168 |
+
"safetyRatings": [
|
| 169 |
+
{
|
| 170 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 171 |
+
"probability": "NEGLIGIBLE"
|
| 172 |
+
},
|
| 173 |
+
{
|
| 174 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 175 |
+
"probability": "NEGLIGIBLE"
|
| 176 |
+
},
|
| 177 |
+
{
|
| 178 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 179 |
+
"probability": "NEGLIGIBLE"
|
| 180 |
+
},
|
| 181 |
+
{
|
| 182 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 183 |
+
"probability": "NEGLIGIBLE"
|
| 184 |
+
}
|
| 185 |
+
],
|
| 186 |
+
"avgLogprobs": -0.59638162092729052
|
| 187 |
+
}
|
| 188 |
+
],
|
| 189 |
+
"usageMetadata": {
|
| 190 |
+
"promptTokenCount": 12,
|
| 191 |
+
"candidatesTokenCount": 88,
|
| 192 |
+
"totalTokenCount": 100,
|
| 193 |
+
"promptTokensDetails": [
|
| 194 |
+
{
|
| 195 |
+
"modality": "TEXT",
|
| 196 |
+
"tokenCount": 12
|
| 197 |
+
}
|
| 198 |
+
],
|
| 199 |
+
"candidatesTokensDetails": [
|
| 200 |
+
{
|
| 201 |
+
"modality": "TEXT",
|
| 202 |
+
"tokenCount": 88
|
| 203 |
+
}
|
| 204 |
+
]
|
| 205 |
+
},
|
| 206 |
+
"modelVersion": "gemini-2.0-flash-exp"
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
**转换后的后端body为(兼容openai的格式):**
|
| 212 |
+
```json
|
| 213 |
+
{
|
| 214 |
+
"id": "chatcmpl-mjGHUQSKJ6gEpH48dP6kk36JPgPic",
|
| 215 |
+
"choices": [
|
| 216 |
+
{
|
| 217 |
+
"index": 0,
|
| 218 |
+
"message": {
|
| 219 |
+
"role": "assistant",
|
| 220 |
+
"content": "你好!我是一个大型语言模型,由 Google 训练。我旨在提供有用的信息和帮助,可以回答你的问题、生成文本、翻译语言、撰写不同类型的创意内容,并以信息丰富的方式回答你的问题。我还在不断学习和改进。\n\n你可以问我任何问题,或者让我完成各种任务。告诉我你需要什么,我会尽力提供帮助。\n"
|
| 221 |
+
},
|
| 222 |
+
"logprobs": null,
|
| 223 |
+
"finish_reason": "stop"
|
| 224 |
+
}
|
| 225 |
+
],
|
| 226 |
+
"created": 1740178551,
|
| 227 |
+
"model": "gemini-2.0-flash-exp",
|
| 228 |
+
"object": "chat.completion",
|
| 229 |
+
"usage": {
|
| 230 |
+
"completion_tokens": 79,
|
| 231 |
+
"prompt_tokens": 12,
|
| 232 |
+
"total_tokens": 91
|
| 233 |
+
}
|
| 234 |
+
}
|
| 235 |
+
```
|
| 236 |
+
|