forked from zons-zhaozhy/update_file_use_ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
150 lines (129 loc) · 4.53 KB
/
Copy pathapi_client.py
File metadata and controls
150 lines (129 loc) · 4.53 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
import abc
import aiohttp
import asyncio
import requests
class ModelAPIClient(abc.ABC):
"""
AI模型API客户端的抽象基类。
定义了所有API客户端都应实现的request方法。
"""
@abc.abstractmethod
async def request(self, content, instruction):
"""
向AI模型发送请求的抽象方法。
参数:
content (str): 要处理的内容
instruction (str): 给AI模型的指令
返回:
dict: API的响应数据
"""
pass
class DeepSeekClient(ModelAPIClient):
"""
DeepSeek API的客户端实现。
"""
def __init__(self, config):
self.base_url = config['base_url']
self.api_key = config['api_key']
self.timeout = aiohttp.ClientTimeout(total=config['timeout'])
async def request(self, content, instruction):
"""
向DeepSeek API发送请求。
参数:
content (str): 要处理的内容
instruction (str): 给AI模型的指令
返回:
dict: API的响应数据
"""
async with aiohttp.ClientSession(timeout=self.timeout) as session:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"content": content,
"instruction": instruction
}
async with session.post(f"{self.base_url}/generate", headers=headers, json=data) as response:
response.raise_for_status()
return await response.json()
class OpenAIClient(ModelAPIClient):
"""
OpenAI API的客户端实现。
"""
def __init__(self, config):
self.api_key = config['api_key']
self.model = config.get('model', 'gpt-3.5-turbo')
self.timeout = aiohttp.ClientTimeout(total=config['timeout'])
async def request(self, content, instruction):
"""
向OpenAI API发送请求。
参数:
content (str): 要处理的内容
instruction (str): 给AI模型的指令
返回:
dict: API的响应数据
"""
async with aiohttp.ClientSession(timeout=self.timeout) as session:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"model": self.model,
"messages": [
{"role": "system", "content": instruction},
{"role": "user", "content": content}
]
}
async with session.post("https://api.openai.com/v1/chat/completions", headers=headers, json=data) as response:
response.raise_for_status()
return await response.json()
class HuggingFaceClient(ModelAPIClient):
"""
HuggingFace API的客户端实现。
"""
def __init__(self, config):
self.api_key = config['api_key']
self.model = config['model']
self.timeout = aiohttp.ClientTimeout(total=config['timeout'])
async def request(self, content, instruction):
"""
向HuggingFace API发送请求。
参数:
content (str): 要处理的内容
instruction (str): 给AI模型的指令
返回:
dict: API的响应数据
"""
async with aiohttp.ClientSession(timeout=self.timeout) as session:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"inputs": f"{instruction}\n\n{content}",
"parameters": {"max_length": 2048}
}
async with session.post(f"https://api-inference.huggingface.co/models/{self.model}", headers=headers, json=data) as response:
response.raise_for_status()
return await response.json()
class APIClient(ModelAPIClient):
"""
用于测试的模拟API客户端。
"""
def __init__(self, config):
self.api_key = config['api_key']
async def request(self, content, instruction):
"""
模拟API请求。
参数:
content (str): 要处理的内容
instruction (str): 给AI模型的指令
返回:
dict: 模拟的API响应数据
"""
# 模拟API请求
return {'result': 'Success'}
# 确保类被正确定义和导出
# 其他模型平台的客户端类也应该继承自 ModelAPIClient 并实现 request 方法。