-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
334 lines (275 loc) · 13 KB
/
Copy pathapi_client.py
File metadata and controls
334 lines (275 loc) · 13 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""API client for external AI services (OpenAI-compatible, custom APIs)."""
import streamlit as st
import requests
import json
from typing import Dict, Any, Tuple, Optional, List
from dataclasses import dataclass
@dataclass
class APIConfig:
"""Configuration for API connection."""
provider: str
endpoint: str
api_key: Optional[str] = None
api_type: str = "openai_compatible"
model_name: Optional[str] = None
class ExternalAPIClient:
"""Client for external AI APIs."""
def __init__(self, config: APIConfig):
self.config = config
self.session = requests.Session()
# Set headers based on API type
if config.api_key:
if config.api_type == "openai_compatible":
self.session.headers.update({
"Authorization": f"Bearer {config.api_key}",
"Content-Type": "application/json"
})
elif config.api_type == "anthropic":
self.session.headers.update({
"x-api-key": config.api_key,
"Content-Type": "application/json",
"anthropic-version": "2023-06-01"
})
def test_connection(self, model_name: Optional[str] = None) -> Tuple[bool, str]:
"""Test API connection."""
try:
if self.config.provider == "agentic_pipeline":
return self._test_agentic_pipeline()
elif self.config.api_type == "openai_compatible":
return self._test_openai_compatible(model_name)
elif self.config.api_type == "anthropic":
return self._test_anthropic(model_name)
else:
return self._test_custom_api(model_name)
except requests.exceptions.RequestException as e:
return False, f"Connection error: {str(e)}"
except Exception as e:
return False, f"Unexpected error: {str(e)}"
def _test_openai_compatible(self, model_name: Optional[str] = None) -> Tuple[bool, str]:
"""Test OpenAI-compatible API."""
# Try to list models first
try:
models_url = f"{self.config.endpoint.rstrip('/')}/models"
response = self.session.get(models_url, timeout=10)
if response.status_code == 200:
models_data = response.json()
if "data" in models_data:
available_models = [model["id"] for model in models_data["data"]]
if model_name and model_name not in available_models:
return False, f"Model '{model_name}' not found. Available: {', '.join(available_models[:5])}"
return True, f"Connected successfully. {len(available_models)} models available."
return True, "Connected successfully."
else:
# If models endpoint fails, try a simple chat completion
return self._test_chat_completion(model_name or "gpt-3.5-turbo")
except Exception:
# Fallback to chat completion test
return self._test_chat_completion(model_name or "gpt-3.5-turbo")
def _test_chat_completion(self, model_name: str) -> Tuple[bool, str]:
"""Test with a simple chat completion."""
chat_url = f"{self.config.endpoint.rstrip('/')}/chat/completions"
test_payload = {
"model": model_name,
"messages": [{"role": "user", "content": "test"}],
"max_tokens": 1,
"temperature": 0
}
response = self.session.post(chat_url, json=test_payload, timeout=10)
if response.status_code == 200:
return True, f"Chat completion test successful with model '{model_name}'"
elif response.status_code == 401:
return False, "Authentication failed. Check your API key."
elif response.status_code == 404:
return False, f"Model '{model_name}' not found or endpoint incorrect."
else:
return False, f"API error: {response.status_code} - {response.text[:200]}"
def _test_anthropic(self, model_name: Optional[str] = None) -> Tuple[bool, str]:
"""Test Anthropic API."""
# Anthropic uses a different endpoint structure
url = f"{self.config.endpoint.rstrip('/')}/messages"
test_payload = {
"model": model_name or "claude-3-sonnet-20240229",
"max_tokens": 1,
"messages": [{"role": "user", "content": "test"}]
}
response = self.session.post(url, json=test_payload, timeout=10)
if response.status_code == 200:
return True, f"Anthropic API test successful with model '{model_name or 'default'}'"
elif response.status_code == 401:
return False, "Authentication failed. Check your API key."
else:
return False, f"API error: {response.status_code} - {response.text[:200]}"
def _test_agentic_pipeline(self) -> Tuple[bool, str]:
"""Test agentic pipeline connection."""
# First try a health/status endpoint
try:
# Try common health check endpoints
health_endpoints = [
f"{self.config.endpoint.rstrip('/')}/health",
f"{self.config.endpoint.rstrip('/')}/status",
f"{self.config.endpoint.rstrip('/')}/ping"
]
for health_url in health_endpoints:
try:
response = self.session.get(health_url, timeout=5)
if response.status_code == 200:
return True, f"Agentic pipeline is healthy (via {health_url.split('/')[-1]})"
except:
continue
# If no health endpoint, try the main chat endpoint with a test message
test_payload = {
"message": "test",
"session_id": "test-connection"
}
response = self.session.post(self.config.endpoint, json=test_payload, timeout=10)
if response.status_code == 200:
return True, "Agentic pipeline chat endpoint is responding"
elif response.status_code == 401:
return False, "Authentication required. Check your API key."
elif response.status_code == 404:
return False, "Chat endpoint not found. Check your endpoint URL."
else:
return False, f"Pipeline returned status {response.status_code}"
except requests.exceptions.ConnectionError:
return False, "Could not connect to agentic pipeline. Check endpoint URL."
except requests.exceptions.Timeout:
return False, "Connection timeout. Pipeline may be slow to respond."
except Exception as e:
return False, f"Pipeline test failed: {str(e)}"
def _test_custom_api(self, model_name: Optional[str] = None) -> Tuple[bool, str]:
"""Test custom API with basic health check."""
# Try a simple GET request first
try:
response = self.session.get(self.config.endpoint, timeout=10)
if response.status_code < 400:
return True, "Custom API endpoint is reachable"
else:
return False, f"Custom API returned status {response.status_code}"
except Exception as e:
return False, f"Custom API test failed: {str(e)}"
def generate_response(self, messages: List[Dict[str, str]], **kwargs) -> str:
"""Generate response from the API."""
if self.config.provider == "agentic_pipeline":
return self._generate_agentic_pipeline(messages, **kwargs)
elif self.config.api_type == "openai_compatible":
return self._generate_openai_compatible(messages, **kwargs)
elif self.config.api_type == "anthropic":
return self._generate_anthropic(messages, **kwargs)
else:
raise NotImplementedError(f"Generation not implemented for {self.config.api_type}")
def _generate_openai_compatible(self, messages: List[Dict[str, str]], **kwargs) -> str:
"""Generate response using OpenAI-compatible API."""
url = f"{self.config.endpoint.rstrip('/')}/chat/completions"
payload = {
"model": self.config.model_name,
"messages": messages,
"temperature": kwargs.get("temperature", 0.7),
"max_tokens": kwargs.get("max_tokens", 2048),
"stream": False
}
response = self.session.post(url, json=payload, timeout=60)
response.raise_for_status()
data = response.json()
return data["choices"][0]["message"]["content"]
def _generate_anthropic(self, messages: List[Dict[str, str]], **kwargs) -> str:
"""Generate response using Anthropic API."""
url = f"{self.config.endpoint.rstrip('/')}/messages"
payload = {
"model": self.config.model_name,
"messages": messages,
"max_tokens": kwargs.get("max_tokens", 2048),
"temperature": kwargs.get("temperature", 0.7)
}
response = self.session.post(url, json=payload, timeout=60)
response.raise_for_status()
data = response.json()
return data["content"][0]["text"]
def _generate_agentic_pipeline(self, messages: List[Dict[str, str]], **kwargs) -> str:
"""Generate response using agentic pipeline."""
import streamlit as st
import uuid
# Get the latest user message
user_message = messages[-1]["content"] if messages else ""
# Get session ID from session state or generate one
session_id = st.session_state.get("agent_session_id", "")
if not session_id:
session_id = str(uuid.uuid4())
st.session_state.agent_session_id = session_id
# Prepare payload for agentic pipeline
payload = {
"message": user_message,
"session_id": session_id
}
# Add pipeline ID if specified
pipeline_id = self.config.model_name
if pipeline_id:
payload["pipeline_id"] = pipeline_id
# Add conversation history if the endpoint supports it
if len(messages) > 1:
payload["conversation_history"] = messages[:-1] # All except the latest message
# Add optional parameters
if kwargs.get("temperature"):
payload["temperature"] = kwargs["temperature"]
if kwargs.get("max_tokens"):
payload["max_tokens"] = kwargs["max_tokens"]
response = self.session.post(self.config.endpoint, json=payload, timeout=120)
response.raise_for_status()
data = response.json()
# Handle different response formats
if "response" in data:
return data["response"]
elif "message" in data:
return data["message"]
elif "content" in data:
return data["content"]
elif "text" in data:
return data["text"]
else:
# Fallback: try to find any string value in the response
for key, value in data.items():
if isinstance(value, str) and len(value) > 0:
return value
raise ValueError(f"Could not parse response from agentic pipeline: {data}")
def get_api_client() -> Optional[ExternalAPIClient]:
"""Get configured API client from session state."""
provider = st.session_state.get("api_provider")
if provider == "ollama":
return None
endpoint = st.session_state.get("api_endpoint")
api_key = st.session_state.get("api_key")
model_name = st.session_state.get("external_model_name")
api_type = st.session_state.get("api_type", "openai_compatible")
if not endpoint:
return None
if provider == "agentic_pipeline":
api_type = "agentic_pipeline"
elif provider == "custom":
api_type = api_type
else:
api_type = "openai_compatible"
config = APIConfig(
provider=provider,
endpoint=endpoint,
api_key=api_key,
api_type=api_type,
model_name=model_name
)
return ExternalAPIClient(config)
def test_api_connection(provider: str, endpoint: str, api_key: Optional[str] = None,
model_name: Optional[str] = None) -> Tuple[bool, str]:
"""Test API connection with given parameters."""
if provider == "agentic_pipeline":
api_type = "agentic_pipeline"
elif provider == "openai_compatible":
api_type = "openai_compatible"
else:
api_type = provider
config = APIConfig(
provider=provider,
endpoint=endpoint,
api_key=api_key,
api_type=api_type,
model_name=model_name
)
client = ExternalAPIClient(config)
return client.test_connection(model_name)