-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathllm_venice.py
234 lines (195 loc) · 7.3 KB
/
llm_venice.py
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
import json
from typing import Optional, Union
import click
import httpx
import llm
from llm.default_plugins.openai_models import Chat
try:
# Pydantic 2
from pydantic import field_validator, Field # type: ignore
except ImportError:
# Pydantic 1
from pydantic.fields import Field
from pydantic.class_validators import validator as field_validator # type: ignore [no-redef]
MODELS = (
"deepseek-r1-671b",
"deepseek-r1-llama-70b",
"dolphin-2.9.2-qwen2-72b",
"llama-3.1-405b",
"llama-3.2-3b",
"llama-3.3-70b",
"qwen32b",
)
class VeniceChatOptions(Chat.Options):
extra_body: Optional[Union[dict, str]] = Field(
description=(
"Additional JSON properties to include in the request body. "
"When provided via CLI, must be a valid JSON string."
),
default=None,
)
@field_validator("extra_body")
def validate_extra_body(cls, extra_body):
if extra_body is None:
return None
if isinstance(extra_body, str):
try:
return json.loads(extra_body)
except json.JSONDecodeError:
raise ValueError("Invalid JSON in extra_body string")
if not isinstance(extra_body, dict):
raise ValueError("extra_body must be a dictionary")
return extra_body
class VeniceChat(Chat):
needs_key = "venice"
key_env_var = "LLM_VENICE_KEY"
def __str__(self):
return f"Venice Chat: {self.model_id}"
class Options(VeniceChatOptions):
pass
@llm.hookimpl
def register_commands(cli):
@cli.group(name="venice")
def venice():
"llm-venice plugin commands"
@venice.command(name="refresh")
def refresh():
"Refresh the list of models from the Venice API"
key = llm.get_key("", "venice", "LLM_VENICE_KEY")
if not key:
raise click.ClickException("No key found for Venice")
headers = {"Authorization": f"Bearer {key}"}
response = httpx.get("https://api.venice.ai/api/v1/models", headers=headers)
response.raise_for_status()
models = response.json()["data"]
text_models = [model["id"] for model in models if model.get("type") == "text"]
if not text_models:
raise click.ClickException("No text generation models found")
path = llm.user_dir() / "llm-venice.json"
path.write_text(json.dumps(text_models, indent=4))
click.echo(f"{len(text_models)} models saved to {path}", err=True)
click.echo(json.dumps(text_models, indent=4))
@click.group(name="api-keys", invoke_without_command=True)
@click.pass_context
def api_keys(ctx):
"""Manage API keys - list, or rate-limits"""
# Retrieve the API key once and store it in context
key = llm.get_key("", "venice", "LLM_VENICE_KEY")
if not key:
raise click.ClickException("No key found for Venice")
ctx.obj = {"headers": {"Authorization": f"Bearer {key}"}}
# Default to listing API keys if no subcommand is provided
if not ctx.invoked_subcommand:
ctx.invoke(list_keys)
@api_keys.command(name="list")
@click.pass_context
def list_keys(ctx):
"""List all API keys."""
response = httpx.get(
"https://api.venice.ai/api/v1/api_keys", headers=ctx.obj["headers"]
)
response.raise_for_status()
click.echo(json.dumps(response.json(), indent=2))
@api_keys.command(name="rate-limits")
@click.pass_context
def rate_limits(ctx):
"Show current rate limits for your API key"
response = httpx.get(
"https://api.venice.ai/api/v1/api_keys", headers=ctx.obj["headers"]
)
response.raise_for_status()
click.echo(json.dumps(response.json(), indent=2))
# Register api-keys command group under "venice"
venice.add_command(api_keys)
# Remove and store the original prompt and chat commands
original_prompt = cli.commands.pop("prompt")
original_chat = cli.commands.pop("chat")
def process_venice_options(kwargs):
"""Helper to process venice-specific options"""
no_venice_system_prompt = kwargs.pop("no_venice_system_prompt", False)
character = kwargs.pop("character", None)
options = list(kwargs.get("options", []))
model = kwargs.get("model_id")
if model and model.startswith("venice/"):
venice_params = {}
if no_venice_system_prompt:
venice_params["include_venice_system_prompt"] = False
if character:
venice_params["character_slug"] = character
if venice_params:
# If a Venice option is used, any `-o extra_body value` is overridden here.
# TODO: Would prefer to remove the extra_body CLI option, but
# the implementation is required for venice_parameters.
options.append(("extra_body", {"venice_parameters": venice_params}))
kwargs["options"] = options
return kwargs
# Create new prompt command
@cli.command(name="prompt")
@click.option(
"--no-venice-system-prompt",
is_flag=True,
help="Disable Venice AI's default system prompt",
)
@click.option(
"--character",
help="Use a Venice AI public character (e.g. 'alan-watts')",
)
@click.pass_context
def new_prompt(ctx, no_venice_system_prompt, character, **kwargs):
"""Execute a prompt"""
kwargs = process_venice_options(
{
**kwargs,
"no_venice_system_prompt": no_venice_system_prompt,
"character": character,
}
)
return ctx.invoke(original_prompt, **kwargs)
# Create new chat command
@cli.command(name="chat")
@click.option(
"--no-venice-system-prompt",
is_flag=True,
help="Disable Venice AI's default system prompt",
)
@click.option(
"--character",
help="Use a Venice AI character (e.g. 'alan-watts')",
)
@click.pass_context
def new_chat(ctx, no_venice_system_prompt, character, **kwargs):
"""Hold an ongoing chat with a model"""
kwargs = process_venice_options(
{
**kwargs,
"no_venice_system_prompt": no_venice_system_prompt,
"character": character,
}
)
return ctx.invoke(original_chat, **kwargs)
# Copy over all params from original commands
for param in original_prompt.params:
if param.name not in ("no_venice_system_prompt", "character"):
new_prompt.params.append(param)
for param in original_chat.params:
if param.name not in ("no_venice_system_prompt", "character"):
new_chat.params.append(param)
@llm.hookimpl
def register_models(register):
key = llm.get_key("", "venice", "LLM_VENICE_KEY")
if not key:
return
path = llm.user_dir() / "llm-venice.json"
if path.exists():
model_ids = json.loads(path.read_text())
else:
model_ids = MODELS
for model_id in model_ids:
register(
VeniceChat(
model_id=f"venice/{model_id}",
model_name=model_id,
api_base="https://api.venice.ai/api/v1",
can_stream=True,
)
)