yym68686 commited on
Commit
2a56a92
·
1 Parent(s): ffc6707

Fix the bug causing errors in the GPT channel when using the Wu API.

Browse files
Files changed (3) hide show
  1. request.py +2 -1
  2. test/aws-boto3.py +24 -0
  3. test/aws.py +90 -0
request.py CHANGED
@@ -118,9 +118,10 @@ async def get_gemini_payload(request, engine, provider):
118
 
119
  async def get_gpt_payload(request, engine, provider):
120
  headers = {
121
- 'Authorization': f"Bearer {provider['api']}",
122
  'Content-Type': 'application/json'
123
  }
 
 
124
  url = provider['base_url']
125
 
126
  messages = []
 
118
 
119
  async def get_gpt_payload(request, engine, provider):
120
  headers = {
 
121
  'Content-Type': 'application/json'
122
  }
123
+ if provider.get("api"):
124
+ headers['Authorization'] = f"Bearer {provider['api']}"
125
  url = provider['base_url']
126
 
127
  messages = []
test/aws-boto3.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import boto3
2
+ import json
3
+ import os
4
+
5
+ AWS_ACCESS_KEY = os.environ.get('AWS_ACCESS_KEY')
6
+ AWS_SECRET_KEY = os.environ.get('AWS_SECRET_KEY')
7
+
8
+ bedrock = boto3.client(
9
+ service_name="bedrock-runtime",
10
+ region_name="us-east-1",
11
+ aws_access_key_id=AWS_ACCESS_KEY,
12
+ aws_secret_access_key=AWS_SECRET_KEY
13
+ )
14
+
15
+ body = json.dumps({
16
+ "max_tokens": 4096,
17
+ "messages": [{"role": "user", "content": "Hello, world"}],
18
+ "anthropic_version": "bedrock-2023-05-31"
19
+ })
20
+
21
+ response = bedrock.invoke_model(body=body, modelId="anthropic.claude-3-5-sonnet-20240620-v1:0")
22
+
23
+ response_body = json.loads(response.get("body").read())
24
+ print(response_body.get("content"))
test/aws.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpx
2
+ import json
3
+ from datetime import datetime
4
+ import hashlib
5
+ import hmac
6
+ import base64
7
+ import os
8
+
9
+ # AWS凭证和配置
10
+ AWS_ACCESS_KEY = os.environ.get('AWS_ACCESS_KEY')
11
+ AWS_SECRET_KEY = os.environ.get('AWS_SECRET_KEY')
12
+ REGION = 'us-east-1'
13
+ SERVICE = 'bedrock'
14
+ ENDPOINT = f'https://bedrock-runtime.{REGION}.amazonaws.com'
15
+
16
+ # 辅助函数用于生成AWS SigV4签名
17
+ def sign(key, msg):
18
+ return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
19
+
20
+ def getSignatureKey(key, dateStamp, regionName, serviceName):
21
+ kDate = sign(('AWS4' + key).encode('utf-8'), dateStamp)
22
+ kRegion = sign(kDate, regionName)
23
+ kService = sign(kRegion, serviceName)
24
+ kSigning = sign(kService, 'aws4_request')
25
+ return kSigning
26
+
27
+ def create_authorization_header(method, url, payload, headers):
28
+ t = datetime.utcnow()
29
+ amzdate = t.strftime('%Y%m%dT%H%M%SZ')
30
+ datestamp = t.strftime('%Y%m%d')
31
+
32
+ canonical_uri = url.split(ENDPOINT)[1]
33
+ canonical_querystring = ''
34
+ canonical_headers = '\n'.join([f"{h.lower()}:{headers[h]}" for h in sorted(headers)]) + '\n'
35
+ signed_headers = ';'.join([h.lower() for h in sorted(headers)])
36
+ payload_hash = hashlib.sha256(payload.encode('utf-8')).hexdigest()
37
+
38
+ canonical_request = f"{method}\n{canonical_uri}\n{canonical_querystring}\n{canonical_headers}\n{signed_headers}\n{payload_hash}"
39
+
40
+ algorithm = 'AWS4-HMAC-SHA256'
41
+ credential_scope = f"{datestamp}/{REGION}/{SERVICE}/aws4_request"
42
+ string_to_sign = f"{algorithm}\n{amzdate}\n{credential_scope}\n{hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()}"
43
+
44
+ signing_key = getSignatureKey(AWS_SECRET_KEY, datestamp, REGION, SERVICE)
45
+ signature = hmac.new(signing_key, string_to_sign.encode('utf-8'), hashlib.sha256).hexdigest()
46
+
47
+ authorization_header = (
48
+ f"{algorithm} "
49
+ f"Credential={AWS_ACCESS_KEY}/{credential_scope}, "
50
+ f"SignedHeaders={signed_headers}, "
51
+ f"Signature={signature}"
52
+ )
53
+
54
+ return authorization_header
55
+
56
+ # 主函数
57
+ def invoke_bedrock_model():
58
+ url = f"{ENDPOINT}/model/anthropic.claude-3-sonnet-20240229-v1:0/invoke"
59
+
60
+ payload = json.dumps({
61
+ "max_tokens": 256,
62
+ "messages": [{"role": "user", "content": "Hello, world"}],
63
+ "anthropic_version": "bedrock-2023-05-31"
64
+ })
65
+
66
+ t = datetime.utcnow()
67
+ amzdate = t.strftime('%Y%m%dT%H%M%SZ')
68
+
69
+ headers = {
70
+ 'Content-Type': 'application/json',
71
+ 'Accept': 'application/json',
72
+ 'X-Amz-Date': amzdate,
73
+ 'Host': f'bedrock-runtime.{REGION}.amazonaws.com'
74
+ }
75
+
76
+ authorization_header = create_authorization_header('POST', url, payload, headers)
77
+ headers['Authorization'] = authorization_header
78
+
79
+ with httpx.Client() as client:
80
+ response = client.post(url, headers=headers, data=payload)
81
+
82
+ if response.status_code == 200:
83
+ response_body = response.json()
84
+ print(response_body.get("content"))
85
+ else:
86
+ print(f"Error: {response.status_code}")
87
+ print(response.text)
88
+
89
+ # 运行函数
90
+ invoke_bedrock_model()