webscout 7.9__py3-none-any.whl → 8.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of webscout might be problematic. Click here for more details.
- webscout/Extra/GitToolkit/__init__.py +10 -0
- webscout/Extra/GitToolkit/gitapi/__init__.py +12 -0
- webscout/Extra/GitToolkit/gitapi/repository.py +195 -0
- webscout/Extra/GitToolkit/gitapi/user.py +96 -0
- webscout/Extra/GitToolkit/gitapi/utils.py +62 -0
- webscout/Extra/YTToolkit/ytapi/video.py +232 -103
- webscout/Provider/AISEARCH/DeepFind.py +1 -1
- webscout/Provider/AISEARCH/ISou.py +1 -1
- webscout/Provider/AISEARCH/__init__.py +6 -1
- webscout/Provider/AISEARCH/felo_search.py +1 -1
- webscout/Provider/AISEARCH/genspark_search.py +1 -1
- webscout/Provider/AISEARCH/hika_search.py +194 -0
- webscout/Provider/AISEARCH/iask_search.py +436 -0
- webscout/Provider/AISEARCH/monica_search.py +246 -0
- webscout/Provider/AISEARCH/scira_search.py +320 -0
- webscout/Provider/AISEARCH/webpilotai_search.py +281 -0
- webscout/Provider/AllenAI.py +255 -122
- webscout/Provider/DeepSeek.py +1 -2
- webscout/Provider/Deepinfra.py +17 -9
- webscout/Provider/ExaAI.py +261 -0
- webscout/Provider/ExaChat.py +8 -1
- webscout/Provider/GithubChat.py +2 -1
- webscout/Provider/Jadve.py +2 -2
- webscout/Provider/Netwrck.py +3 -2
- webscout/Provider/OPENAI/__init__.py +17 -0
- webscout/Provider/OPENAI/base.py +46 -0
- webscout/Provider/OPENAI/c4ai.py +347 -0
- webscout/Provider/OPENAI/chatgptclone.py +460 -0
- webscout/Provider/OPENAI/deepinfra.py +284 -0
- webscout/Provider/OPENAI/exaai.py +419 -0
- webscout/Provider/OPENAI/exachat.py +421 -0
- webscout/Provider/OPENAI/freeaichat.py +355 -0
- webscout/Provider/OPENAI/glider.py +314 -0
- webscout/Provider/OPENAI/heckai.py +337 -0
- webscout/Provider/OPENAI/llmchatco.py +325 -0
- webscout/Provider/OPENAI/netwrck.py +348 -0
- webscout/Provider/OPENAI/scirachat.py +459 -0
- webscout/Provider/OPENAI/sonus.py +294 -0
- webscout/Provider/OPENAI/typegpt.py +361 -0
- webscout/Provider/OPENAI/utils.py +211 -0
- webscout/Provider/OPENAI/venice.py +428 -0
- webscout/Provider/OPENAI/wisecat.py +381 -0
- webscout/Provider/OPENAI/x0gpt.py +389 -0
- webscout/Provider/OPENAI/yep.py +329 -0
- webscout/Provider/OpenGPT.py +199 -0
- webscout/Provider/PI.py +39 -24
- webscout/Provider/Venice.py +1 -1
- webscout/Provider/Youchat.py +326 -296
- webscout/Provider/__init__.py +16 -6
- webscout/Provider/ai4chat.py +58 -56
- webscout/Provider/akashgpt.py +34 -22
- webscout/Provider/freeaichat.py +1 -1
- webscout/Provider/labyrinth.py +121 -20
- webscout/Provider/llmchatco.py +306 -0
- webscout/Provider/scira_chat.py +274 -0
- webscout/Provider/typefully.py +280 -0
- webscout/Provider/typegpt.py +3 -184
- webscout/prompt_manager.py +2 -1
- webscout/version.py +1 -1
- webscout/webscout_search.py +118 -54
- webscout/webscout_search_async.py +109 -45
- webscout-8.1.dist-info/METADATA +683 -0
- {webscout-7.9.dist-info → webscout-8.1.dist-info}/RECORD +67 -33
- webscout/Provider/flowith.py +0 -207
- webscout-7.9.dist-info/METADATA +0 -995
- {webscout-7.9.dist-info → webscout-8.1.dist-info}/LICENSE.md +0 -0
- {webscout-7.9.dist-info → webscout-8.1.dist-info}/WHEEL +0 -0
- {webscout-7.9.dist-info → webscout-8.1.dist-info}/entry_points.txt +0 -0
- {webscout-7.9.dist-info → webscout-8.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
from typing import List, Dict, Optional, Any, Union
|
|
2
|
+
from dataclasses import dataclass, asdict, is_dataclass
|
|
3
|
+
from enum import Enum
|
|
4
|
+
|
|
5
|
+
# --- OpenAI Response Structure Mimics ---
|
|
6
|
+
# Moved here for reusability across different OpenAI-compatible providers
|
|
7
|
+
|
|
8
|
+
class ToolCallType(str, Enum):
|
|
9
|
+
"""Type of tool call."""
|
|
10
|
+
FUNCTION = "function"
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class BaseModel:
|
|
14
|
+
"""Base class for all models."""
|
|
15
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
16
|
+
"""Convert the model to a dictionary."""
|
|
17
|
+
def _convert(obj: Any) -> Any:
|
|
18
|
+
if is_dataclass(obj):
|
|
19
|
+
return {k: _convert(v) for k, v in asdict(obj).items() if v is not None}
|
|
20
|
+
elif isinstance(obj, list):
|
|
21
|
+
return [_convert(item) for item in obj]
|
|
22
|
+
elif isinstance(obj, dict):
|
|
23
|
+
return {k: _convert(v) for k, v in obj.items() if v is not None}
|
|
24
|
+
elif isinstance(obj, Enum):
|
|
25
|
+
return obj.value
|
|
26
|
+
return obj
|
|
27
|
+
return _convert(self)
|
|
28
|
+
|
|
29
|
+
def __getitem__(self, key):
|
|
30
|
+
"""Support dictionary-style access."""
|
|
31
|
+
if hasattr(self, key):
|
|
32
|
+
return getattr(self, key)
|
|
33
|
+
raise KeyError(f"{key} not found in {self.__class__.__name__}")
|
|
34
|
+
|
|
35
|
+
def get(self, key, default=None):
|
|
36
|
+
"""Dictionary-style get method with default value."""
|
|
37
|
+
try:
|
|
38
|
+
return self[key]
|
|
39
|
+
except KeyError:
|
|
40
|
+
return default
|
|
41
|
+
|
|
42
|
+
def __contains__(self, key):
|
|
43
|
+
"""Support 'in' operator."""
|
|
44
|
+
return hasattr(self, key)
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class FunctionCall(BaseModel):
|
|
48
|
+
"""Function call specification."""
|
|
49
|
+
name: str
|
|
50
|
+
arguments: str
|
|
51
|
+
|
|
52
|
+
@dataclass
|
|
53
|
+
class ToolFunction(BaseModel):
|
|
54
|
+
"""Function specification in a tool."""
|
|
55
|
+
name: str
|
|
56
|
+
arguments: str
|
|
57
|
+
|
|
58
|
+
@dataclass
|
|
59
|
+
class ToolCall(BaseModel):
|
|
60
|
+
"""Tool call specification."""
|
|
61
|
+
id: str
|
|
62
|
+
type: str
|
|
63
|
+
function: ToolFunction
|
|
64
|
+
|
|
65
|
+
@dataclass
|
|
66
|
+
class CompletionUsage(BaseModel):
|
|
67
|
+
"""Token usage information."""
|
|
68
|
+
prompt_tokens: int
|
|
69
|
+
completion_tokens: int
|
|
70
|
+
total_tokens: int
|
|
71
|
+
prompt_tokens_details: Optional[Dict[str, Any]] = None
|
|
72
|
+
|
|
73
|
+
@dataclass
|
|
74
|
+
class ChoiceDelta(BaseModel):
|
|
75
|
+
"""Delta content in streaming response."""
|
|
76
|
+
content: Optional[str] = None
|
|
77
|
+
function_call: Optional[FunctionCall] = None
|
|
78
|
+
role: Optional[str] = None
|
|
79
|
+
tool_calls: Optional[List[ToolCall]] = None
|
|
80
|
+
|
|
81
|
+
@dataclass
|
|
82
|
+
class ChatCompletionMessage(BaseModel):
|
|
83
|
+
"""Chat message in completion response."""
|
|
84
|
+
role: str
|
|
85
|
+
content: Optional[str] = None
|
|
86
|
+
function_call: Optional[FunctionCall] = None
|
|
87
|
+
tool_calls: Optional[List[ToolCall]] = None
|
|
88
|
+
|
|
89
|
+
@dataclass
|
|
90
|
+
class Choice(BaseModel):
|
|
91
|
+
"""Choice in completion response."""
|
|
92
|
+
index: int
|
|
93
|
+
message: Optional[ChatCompletionMessage] = None
|
|
94
|
+
delta: Optional[ChoiceDelta] = None
|
|
95
|
+
finish_reason: Optional[str] = None
|
|
96
|
+
logprobs: Optional[Dict[str, Any]] = None
|
|
97
|
+
|
|
98
|
+
@dataclass
|
|
99
|
+
class ChatCompletion(BaseModel):
|
|
100
|
+
"""Chat completion response."""
|
|
101
|
+
id: str
|
|
102
|
+
created: int
|
|
103
|
+
model: str
|
|
104
|
+
choices: List[Choice]
|
|
105
|
+
object: str = "chat.completion"
|
|
106
|
+
system_fingerprint: Optional[str] = None
|
|
107
|
+
usage: Optional[CompletionUsage] = None
|
|
108
|
+
|
|
109
|
+
@dataclass
|
|
110
|
+
class ChatCompletionChunk(BaseModel):
|
|
111
|
+
"""Streaming chat completion response chunk."""
|
|
112
|
+
id: str
|
|
113
|
+
created: int
|
|
114
|
+
model: str
|
|
115
|
+
choices: List[Choice]
|
|
116
|
+
object: str = "chat.completion.chunk"
|
|
117
|
+
system_fingerprint: Optional[str] = None
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
# --- Helper Functions ---
|
|
121
|
+
|
|
122
|
+
def format_prompt(messages: List[Dict[str, Any]], add_special_tokens: bool = False,
|
|
123
|
+
do_continue: bool = False, include_system: bool = True) -> str:
|
|
124
|
+
"""
|
|
125
|
+
Format a series of messages into a single string, optionally adding special tokens.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
messages: A list of message dictionaries, each containing 'role' and 'content'.
|
|
129
|
+
add_special_tokens: Whether to add special formatting tokens.
|
|
130
|
+
do_continue: If True, don't add the final "Assistant:" prompt.
|
|
131
|
+
include_system: Whether to include system messages in the formatted output.
|
|
132
|
+
|
|
133
|
+
Returns:
|
|
134
|
+
A formatted string containing all messages.
|
|
135
|
+
"""
|
|
136
|
+
# Helper function to convert content to string
|
|
137
|
+
def to_string(value) -> str:
|
|
138
|
+
if isinstance(value, str):
|
|
139
|
+
return value
|
|
140
|
+
elif isinstance(value, dict):
|
|
141
|
+
if "text" in value:
|
|
142
|
+
return value.get("text", "")
|
|
143
|
+
return ""
|
|
144
|
+
elif isinstance(value, list):
|
|
145
|
+
return "".join([to_string(v) for v in value])
|
|
146
|
+
return str(value)
|
|
147
|
+
|
|
148
|
+
# If there's only one message and no special tokens needed, just return its content
|
|
149
|
+
if not add_special_tokens and len(messages) <= 1:
|
|
150
|
+
return to_string(messages[0]["content"])
|
|
151
|
+
|
|
152
|
+
# Filter and process messages
|
|
153
|
+
processed_messages = [
|
|
154
|
+
(message["role"], to_string(message["content"]))
|
|
155
|
+
for message in messages
|
|
156
|
+
if include_system or message.get("role") != "system"
|
|
157
|
+
]
|
|
158
|
+
|
|
159
|
+
# Format each message as "Role: Content"
|
|
160
|
+
formatted = "\n".join([
|
|
161
|
+
f'{role.capitalize()}: {content}'
|
|
162
|
+
for role, content in processed_messages
|
|
163
|
+
if content.strip()
|
|
164
|
+
])
|
|
165
|
+
|
|
166
|
+
# Add final prompt for assistant if needed
|
|
167
|
+
if do_continue:
|
|
168
|
+
return formatted
|
|
169
|
+
|
|
170
|
+
return f"{formatted}\nAssistant:"
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def get_system_prompt(messages: List[Dict[str, Any]]) -> str:
|
|
174
|
+
"""
|
|
175
|
+
Extract and concatenate all system messages.
|
|
176
|
+
|
|
177
|
+
Args:
|
|
178
|
+
messages: A list of message dictionaries.
|
|
179
|
+
|
|
180
|
+
Returns:
|
|
181
|
+
A string containing all system messages concatenated with newlines.
|
|
182
|
+
"""
|
|
183
|
+
return "\n".join([m["content"] for m in messages if m["role"] == "system"])
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def get_last_user_message(messages: List[Dict[str, Any]]) -> str:
|
|
187
|
+
"""
|
|
188
|
+
Get the content of the last user message in the conversation.
|
|
189
|
+
|
|
190
|
+
Args:
|
|
191
|
+
messages: A list of message dictionaries.
|
|
192
|
+
|
|
193
|
+
Returns:
|
|
194
|
+
The content of the last user message as a string.
|
|
195
|
+
"""
|
|
196
|
+
for message in reversed(messages):
|
|
197
|
+
if message["role"] == "user":
|
|
198
|
+
if isinstance(message["content"], str):
|
|
199
|
+
return message["content"]
|
|
200
|
+
# Handle complex content structures
|
|
201
|
+
if isinstance(message["content"], dict) and "text" in message["content"]:
|
|
202
|
+
return message["content"]["text"]
|
|
203
|
+
if isinstance(message["content"], list):
|
|
204
|
+
text_parts = []
|
|
205
|
+
for part in message["content"]:
|
|
206
|
+
if isinstance(part, dict) and part.get("type") == "text":
|
|
207
|
+
text_parts.append(part.get("text", ""))
|
|
208
|
+
elif isinstance(part, str):
|
|
209
|
+
text_parts.append(part)
|
|
210
|
+
return "".join(text_parts)
|
|
211
|
+
return ""
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import uuid
|
|
3
|
+
import requests
|
|
4
|
+
import json
|
|
5
|
+
from typing import List, Dict, Optional, Union, Generator, Any
|
|
6
|
+
|
|
7
|
+
# Import base classes and utility structures
|
|
8
|
+
from .base import OpenAICompatibleProvider, BaseChat, BaseCompletions
|
|
9
|
+
from .utils import (
|
|
10
|
+
ChatCompletionChunk, ChatCompletion, Choice, ChoiceDelta,
|
|
11
|
+
ChatCompletionMessage, CompletionUsage
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
# Attempt to import LitAgent, fallback if not available
|
|
15
|
+
try:
|
|
16
|
+
from webscout.litagent import LitAgent
|
|
17
|
+
except ImportError:
|
|
18
|
+
# Define a dummy LitAgent if webscout is not installed or accessible
|
|
19
|
+
class LitAgent:
|
|
20
|
+
def generate_fingerprint(self, browser: str = "chrome") -> Dict[str, Any]:
|
|
21
|
+
# Return minimal default headers if LitAgent is unavailable
|
|
22
|
+
print("Warning: LitAgent not found. Using default minimal headers.")
|
|
23
|
+
return {
|
|
24
|
+
"accept": "*/*",
|
|
25
|
+
"accept_language": "en-US,en;q=0.9",
|
|
26
|
+
"platform": "Windows",
|
|
27
|
+
"sec_ch_ua": '"Not/A)Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
|
|
28
|
+
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
|
|
29
|
+
"browser_type": browser,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
def random(self) -> str:
|
|
33
|
+
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
|
|
34
|
+
|
|
35
|
+
# --- Venice Client ---
|
|
36
|
+
|
|
37
|
+
class Completions(BaseCompletions):
|
|
38
|
+
def __init__(self, client: 'Venice'):
|
|
39
|
+
self._client = client
|
|
40
|
+
|
|
41
|
+
def create(
|
|
42
|
+
self,
|
|
43
|
+
*,
|
|
44
|
+
model: str,
|
|
45
|
+
messages: List[Dict[str, str]],
|
|
46
|
+
max_tokens: Optional[int] = 2049,
|
|
47
|
+
stream: bool = False,
|
|
48
|
+
temperature: Optional[float] = 0.8,
|
|
49
|
+
top_p: Optional[float] = 0.9,
|
|
50
|
+
**kwargs: Any
|
|
51
|
+
) -> Union[ChatCompletion, Generator[ChatCompletionChunk, None, None]]:
|
|
52
|
+
"""
|
|
53
|
+
Creates a model response for the given chat conversation.
|
|
54
|
+
Mimics openai.chat.completions.create
|
|
55
|
+
"""
|
|
56
|
+
# Extract system message if present for systemPrompt parameter
|
|
57
|
+
system_prompt = self._client.system_prompt
|
|
58
|
+
for msg in messages:
|
|
59
|
+
if msg["role"] == "system":
|
|
60
|
+
system_prompt = msg["content"]
|
|
61
|
+
break
|
|
62
|
+
|
|
63
|
+
# Prepare the payload for Venice API
|
|
64
|
+
payload = {
|
|
65
|
+
"requestId": str(uuid.uuid4())[:7],
|
|
66
|
+
"modelId": self._client.convert_model_name(model),
|
|
67
|
+
"prompt": messages,
|
|
68
|
+
"systemPrompt": system_prompt,
|
|
69
|
+
"conversationType": "text",
|
|
70
|
+
"temperature": temperature if temperature is not None else self._client.temperature,
|
|
71
|
+
"webEnabled": True,
|
|
72
|
+
"topP": top_p if top_p is not None else self._client.top_p,
|
|
73
|
+
"includeVeniceSystemPrompt": False,
|
|
74
|
+
"isCharacter": False,
|
|
75
|
+
"clientProcessingTime": 2000
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
# Add optional parameters if provided
|
|
79
|
+
if max_tokens is not None and max_tokens > 0:
|
|
80
|
+
payload["max_tokens"] = max_tokens
|
|
81
|
+
|
|
82
|
+
# Add any additional parameters
|
|
83
|
+
for key, value in kwargs.items():
|
|
84
|
+
if key not in payload:
|
|
85
|
+
payload[key] = value
|
|
86
|
+
|
|
87
|
+
request_id = f"chatcmpl-{uuid.uuid4()}"
|
|
88
|
+
created_time = int(time.time())
|
|
89
|
+
|
|
90
|
+
if stream:
|
|
91
|
+
return self._create_stream(request_id, created_time, model, payload)
|
|
92
|
+
else:
|
|
93
|
+
return self._create_non_stream(request_id, created_time, model, payload)
|
|
94
|
+
|
|
95
|
+
def _create_stream(
|
|
96
|
+
self, request_id: str, created_time: int, model: str, payload: Dict[str, Any]
|
|
97
|
+
) -> Generator[ChatCompletionChunk, None, None]:
|
|
98
|
+
try:
|
|
99
|
+
response = self._client.session.post(
|
|
100
|
+
self._client.api_endpoint,
|
|
101
|
+
json=payload,
|
|
102
|
+
stream=True,
|
|
103
|
+
timeout=self._client.timeout
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# Handle non-200 responses
|
|
107
|
+
if response.status_code != 200:
|
|
108
|
+
raise IOError(
|
|
109
|
+
f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
# Track token usage across chunks
|
|
113
|
+
prompt_tokens = 0
|
|
114
|
+
completion_tokens = 0
|
|
115
|
+
total_tokens = 0
|
|
116
|
+
|
|
117
|
+
# Estimate prompt tokens based on message length
|
|
118
|
+
prompt_tokens = 0
|
|
119
|
+
for msg in payload.get("prompt", []):
|
|
120
|
+
prompt_tokens += len(msg.get("content", "").split())
|
|
121
|
+
prompt_tokens += len(payload.get("systemPrompt", "").split())
|
|
122
|
+
|
|
123
|
+
for line in response.iter_lines():
|
|
124
|
+
if not line:
|
|
125
|
+
continue
|
|
126
|
+
|
|
127
|
+
try:
|
|
128
|
+
# Decode bytes to string
|
|
129
|
+
line_data = line.decode('utf-8').strip()
|
|
130
|
+
if '"kind":"content"' in line_data:
|
|
131
|
+
data = json.loads(line_data)
|
|
132
|
+
if 'content' in data:
|
|
133
|
+
content = data['content']
|
|
134
|
+
|
|
135
|
+
# Format the content (replace escaped newlines)
|
|
136
|
+
content = self._client.format_text(content)
|
|
137
|
+
|
|
138
|
+
# Update token counts
|
|
139
|
+
completion_tokens += 1
|
|
140
|
+
total_tokens = prompt_tokens + completion_tokens
|
|
141
|
+
|
|
142
|
+
# Create the delta object
|
|
143
|
+
delta = ChoiceDelta(
|
|
144
|
+
content=content,
|
|
145
|
+
role="assistant",
|
|
146
|
+
tool_calls=None
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Create the choice object
|
|
150
|
+
choice = Choice(
|
|
151
|
+
index=0,
|
|
152
|
+
delta=delta,
|
|
153
|
+
finish_reason=None,
|
|
154
|
+
logprobs=None
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
# Create the chunk object
|
|
158
|
+
chunk = ChatCompletionChunk(
|
|
159
|
+
id=request_id,
|
|
160
|
+
choices=[choice],
|
|
161
|
+
created=created_time,
|
|
162
|
+
model=model,
|
|
163
|
+
system_fingerprint=None
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
# Convert to dict for proper formatting
|
|
167
|
+
chunk_dict = chunk.to_dict()
|
|
168
|
+
|
|
169
|
+
# Add usage information to match OpenAI format
|
|
170
|
+
usage_dict = {
|
|
171
|
+
"prompt_tokens": prompt_tokens,
|
|
172
|
+
"completion_tokens": completion_tokens,
|
|
173
|
+
"total_tokens": total_tokens,
|
|
174
|
+
"estimated_cost": None
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
chunk_dict["usage"] = usage_dict
|
|
178
|
+
|
|
179
|
+
# Return the chunk object for internal processing
|
|
180
|
+
yield chunk
|
|
181
|
+
except json.JSONDecodeError:
|
|
182
|
+
continue
|
|
183
|
+
except UnicodeDecodeError:
|
|
184
|
+
continue
|
|
185
|
+
|
|
186
|
+
# Final chunk with finish_reason="stop"
|
|
187
|
+
delta = ChoiceDelta(
|
|
188
|
+
content=None,
|
|
189
|
+
role=None,
|
|
190
|
+
tool_calls=None
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
choice = Choice(
|
|
194
|
+
index=0,
|
|
195
|
+
delta=delta,
|
|
196
|
+
finish_reason="stop",
|
|
197
|
+
logprobs=None
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
chunk = ChatCompletionChunk(
|
|
201
|
+
id=request_id,
|
|
202
|
+
choices=[choice],
|
|
203
|
+
created=created_time,
|
|
204
|
+
model=model,
|
|
205
|
+
system_fingerprint=None
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
chunk_dict = chunk.to_dict()
|
|
209
|
+
chunk_dict["usage"] = {
|
|
210
|
+
"prompt_tokens": prompt_tokens,
|
|
211
|
+
"completion_tokens": completion_tokens,
|
|
212
|
+
"total_tokens": total_tokens,
|
|
213
|
+
"estimated_cost": None
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
yield chunk
|
|
217
|
+
|
|
218
|
+
except Exception as e:
|
|
219
|
+
print(f"Error during Venice stream request: {e}")
|
|
220
|
+
raise IOError(f"Venice request failed: {e}") from e
|
|
221
|
+
|
|
222
|
+
def _create_non_stream(
|
|
223
|
+
self, request_id: str, created_time: int, model: str, payload: Dict[str, Any]
|
|
224
|
+
) -> ChatCompletion:
|
|
225
|
+
try:
|
|
226
|
+
# For non-streaming, we still use streaming internally to collect the full response
|
|
227
|
+
response = self._client.session.post(
|
|
228
|
+
self._client.api_endpoint,
|
|
229
|
+
json=payload,
|
|
230
|
+
stream=True,
|
|
231
|
+
timeout=self._client.timeout
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
# Handle non-200 responses
|
|
235
|
+
if response.status_code != 200:
|
|
236
|
+
raise IOError(
|
|
237
|
+
f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
# Collect the full response
|
|
241
|
+
full_text = ""
|
|
242
|
+
for line in response.iter_lines():
|
|
243
|
+
if not line:
|
|
244
|
+
continue
|
|
245
|
+
|
|
246
|
+
try:
|
|
247
|
+
# Decode bytes to string
|
|
248
|
+
line_data = line.decode('utf-8').strip()
|
|
249
|
+
if '"kind":"content"' in line_data:
|
|
250
|
+
data = json.loads(line_data)
|
|
251
|
+
if 'content' in data:
|
|
252
|
+
content = data['content']
|
|
253
|
+
full_text += content
|
|
254
|
+
except json.JSONDecodeError:
|
|
255
|
+
continue
|
|
256
|
+
except UnicodeDecodeError:
|
|
257
|
+
continue
|
|
258
|
+
|
|
259
|
+
# Format the text (replace escaped newlines)
|
|
260
|
+
full_text = self._client.format_text(full_text)
|
|
261
|
+
|
|
262
|
+
# Estimate token counts
|
|
263
|
+
prompt_tokens = 0
|
|
264
|
+
for msg in payload.get("prompt", []):
|
|
265
|
+
prompt_tokens += len(msg.get("content", "").split())
|
|
266
|
+
prompt_tokens += len(payload.get("systemPrompt", "").split())
|
|
267
|
+
completion_tokens = len(full_text.split())
|
|
268
|
+
total_tokens = prompt_tokens + completion_tokens
|
|
269
|
+
|
|
270
|
+
# Create the message object
|
|
271
|
+
message = ChatCompletionMessage(
|
|
272
|
+
role="assistant",
|
|
273
|
+
content=full_text
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
# Create the choice object
|
|
277
|
+
choice = Choice(
|
|
278
|
+
index=0,
|
|
279
|
+
message=message,
|
|
280
|
+
finish_reason="stop"
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
# Create the usage object
|
|
284
|
+
usage = CompletionUsage(
|
|
285
|
+
prompt_tokens=prompt_tokens,
|
|
286
|
+
completion_tokens=completion_tokens,
|
|
287
|
+
total_tokens=total_tokens
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
# Create the completion object
|
|
291
|
+
completion = ChatCompletion(
|
|
292
|
+
id=request_id,
|
|
293
|
+
choices=[choice],
|
|
294
|
+
created=created_time,
|
|
295
|
+
model=model,
|
|
296
|
+
usage=usage,
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
return completion
|
|
300
|
+
|
|
301
|
+
except Exception as e:
|
|
302
|
+
print(f"Error during Venice non-stream request: {e}")
|
|
303
|
+
raise IOError(f"Venice request failed: {e}") from e
|
|
304
|
+
|
|
305
|
+
class Chat(BaseChat):
|
|
306
|
+
def __init__(self, client: 'Venice'):
|
|
307
|
+
self.completions = Completions(client)
|
|
308
|
+
|
|
309
|
+
class Venice(OpenAICompatibleProvider):
|
|
310
|
+
"""
|
|
311
|
+
OpenAI-compatible client for Venice AI API.
|
|
312
|
+
|
|
313
|
+
Usage:
|
|
314
|
+
client = Venice()
|
|
315
|
+
response = client.chat.completions.create(
|
|
316
|
+
model="mistral-31-24b",
|
|
317
|
+
messages=[{"role": "user", "content": "Hello!"}]
|
|
318
|
+
)
|
|
319
|
+
"""
|
|
320
|
+
|
|
321
|
+
AVAILABLE_MODELS = [
|
|
322
|
+
"mistral-31-24b",
|
|
323
|
+
"llama-3.2-3b-akash",
|
|
324
|
+
"qwen2dot5-coder-32b",
|
|
325
|
+
"deepseek-coder-v2-lite",
|
|
326
|
+
]
|
|
327
|
+
|
|
328
|
+
# No model mapping needed as we use the model names directly
|
|
329
|
+
|
|
330
|
+
def __init__(
|
|
331
|
+
self,
|
|
332
|
+
timeout: Optional[int] = None,
|
|
333
|
+
browser: str = "chrome"
|
|
334
|
+
):
|
|
335
|
+
"""
|
|
336
|
+
Initialize the Venice client.
|
|
337
|
+
|
|
338
|
+
Args:
|
|
339
|
+
timeout: Request timeout in seconds (None for no timeout)
|
|
340
|
+
browser: Browser to emulate in user agent
|
|
341
|
+
"""
|
|
342
|
+
self.timeout = timeout
|
|
343
|
+
self.temperature = 0.8 # Default temperature
|
|
344
|
+
self.top_p = 0.9 # Default top_p
|
|
345
|
+
self.system_prompt = "You are a helpful AI assistant." # Default system prompt
|
|
346
|
+
self.api_endpoint = "https://venice.ai/api/inference/chat"
|
|
347
|
+
self.session = requests.Session()
|
|
348
|
+
|
|
349
|
+
# Initialize LitAgent for user agent generation
|
|
350
|
+
agent = LitAgent()
|
|
351
|
+
self.fingerprint = agent.generate_fingerprint(browser)
|
|
352
|
+
|
|
353
|
+
# Headers for the request
|
|
354
|
+
self.headers = {
|
|
355
|
+
"User-Agent": self.fingerprint["user_agent"],
|
|
356
|
+
"accept": self.fingerprint["accept"],
|
|
357
|
+
"accept-language": self.fingerprint["accept_language"],
|
|
358
|
+
"content-type": "application/json",
|
|
359
|
+
"origin": "https://venice.ai",
|
|
360
|
+
"referer": "https://venice.ai/chat/",
|
|
361
|
+
"sec-ch-ua": self.fingerprint["sec_ch_ua"] or '"Google Chrome";v="133", "Chromium";v="133", "Not?A_Brand";v="24"',
|
|
362
|
+
"sec-ch-ua-mobile": "?0",
|
|
363
|
+
"sec-ch-ua-platform": f'"{self.fingerprint["platform"]}"',
|
|
364
|
+
"sec-fetch-dest": "empty",
|
|
365
|
+
"sec-fetch-mode": "cors",
|
|
366
|
+
"sec-fetch-site": "same-origin"
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
self.session.headers.update(self.headers)
|
|
370
|
+
|
|
371
|
+
# Initialize the chat interface
|
|
372
|
+
self.chat = Chat(self)
|
|
373
|
+
|
|
374
|
+
def format_text(self, text: str) -> str:
|
|
375
|
+
"""
|
|
376
|
+
Format text by replacing escaped newlines with actual newlines.
|
|
377
|
+
|
|
378
|
+
Args:
|
|
379
|
+
text: Text to format
|
|
380
|
+
|
|
381
|
+
Returns:
|
|
382
|
+
Formatted text
|
|
383
|
+
"""
|
|
384
|
+
# Use a more comprehensive approach to handle all escape sequences
|
|
385
|
+
try:
|
|
386
|
+
# First handle double backslashes to avoid issues
|
|
387
|
+
text = text.replace('\\\\', '\\')
|
|
388
|
+
|
|
389
|
+
# Handle common escape sequences
|
|
390
|
+
text = text.replace('\\n', '\n')
|
|
391
|
+
text = text.replace('\\r', '\r')
|
|
392
|
+
text = text.replace('\\t', '\t')
|
|
393
|
+
text = text.replace('\\"', '"')
|
|
394
|
+
text = text.replace("\\'", "'")
|
|
395
|
+
|
|
396
|
+
# Handle any remaining escape sequences using JSON decoding
|
|
397
|
+
# This is a fallback in case there are other escape sequences
|
|
398
|
+
try:
|
|
399
|
+
# Add quotes to make it a valid JSON string
|
|
400
|
+
json_str = f'"{text}"'
|
|
401
|
+
# Use json module to decode all escape sequences
|
|
402
|
+
decoded = json.loads(json_str)
|
|
403
|
+
return decoded
|
|
404
|
+
except json.JSONDecodeError:
|
|
405
|
+
# If JSON decoding fails, return the text with the replacements we've already done
|
|
406
|
+
return text
|
|
407
|
+
except Exception as e:
|
|
408
|
+
# If any error occurs, return the original text
|
|
409
|
+
print(f"Warning: Error formatting text: {e}")
|
|
410
|
+
return text
|
|
411
|
+
|
|
412
|
+
def convert_model_name(self, model: str) -> str:
|
|
413
|
+
"""
|
|
414
|
+
Convert model names to ones supported by Venice.
|
|
415
|
+
|
|
416
|
+
Args:
|
|
417
|
+
model: Model name to convert
|
|
418
|
+
|
|
419
|
+
Returns:
|
|
420
|
+
Venice model name
|
|
421
|
+
"""
|
|
422
|
+
# If the model is already a valid Venice model, return it
|
|
423
|
+
if model in self.AVAILABLE_MODELS:
|
|
424
|
+
return model
|
|
425
|
+
|
|
426
|
+
# Default to the most capable model
|
|
427
|
+
print(f"Warning: Unknown model '{model}'. Using 'mistral-31-24b' instead.")
|
|
428
|
+
return "mistral-31-24b"
|