-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathcopilot_api.py
More file actions
555 lines (453 loc) · 18.3 KB
/
copilot_api.py
File metadata and controls
555 lines (453 loc) · 18.3 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import logging
from app.desktop.studio_server.api_client.kiln_ai_server_client.api.copilot import (
clarify_spec_v1_copilot_clarify_spec_post,
generate_batch_v1_copilot_generate_batch_post,
question_spec_v1_copilot_question_spec_post,
refine_spec_v1_copilot_refine_spec_post,
refine_spec_with_answers_v1_copilot_refine_spec_with_answers_post,
)
from app.desktop.studio_server.api_client.kiln_ai_server_client.models import (
ClarifySpecInput,
ClarifySpecOutput,
GenerateBatchInput,
GenerateBatchOutput,
HTTPValidationError,
RefineSpecInput,
RefineSpecOutput,
)
from app.desktop.studio_server.api_client.kiln_ai_server_client.models import (
QuestionSet as QuestionSetServerApi,
)
from app.desktop.studio_server.api_client.kiln_ai_server_client.models import (
RefineSpecWithQuestionAnswersResponse as RefineSpecWithQuestionAnswersResponseServerApi,
)
from app.desktop.studio_server.api_client.kiln_ai_server_client.models import (
SpecQuestionerInput as SpecQuestionerInputServerApi,
)
from app.desktop.studio_server.api_client.kiln_ai_server_client.models import (
SubmitAnswersRequest as SubmitAnswersRequestServerApi,
)
from app.desktop.studio_server.api_client.kiln_server_client import (
get_authenticated_client,
)
from app.desktop.util.spec_creation import (
NUM_SAMPLES_PER_TOPIC,
NUM_TOPICS,
ReviewedExample,
SampleApi,
create_dataset_task_runs,
spec_eval_data_type,
spec_eval_output_score,
spec_eval_template,
)
from fastapi import FastAPI, HTTPException
from kiln_ai.datamodel import TaskRun
from kiln_ai.datamodel.basemodel import FilenameString
from kiln_ai.datamodel.datamodel_enums import ModelProviderName, Priority
from kiln_ai.datamodel.eval import Eval, EvalConfig, EvalConfigType
from kiln_ai.datamodel.questions import (
QuestionSet,
RefineSpecWithQuestionAnswersResponse,
SpecQuestionerInput,
SubmitAnswersRequest,
)
from kiln_ai.datamodel.spec import Spec, SpecStatus
from kiln_ai.datamodel.spec_properties import SpecProperties
from kiln_ai.utils.config import Config
from kiln_ai.utils.name_generator import generate_memorable_name
from kiln_server.task_api import task_from_id
from pydantic import BaseModel, Field
logger = logging.getLogger(__name__)
# Pydantic input models (replacing attrs-based client models)
class TargetTaskInfoApi(BaseModel):
target_task_prompt: str
target_task_input_schema: str
target_task_output_schema: str
class SpecInfoApi(BaseModel):
spec_fields: dict[str, str]
spec_field_current_values: dict[str, str]
class ExampleWithFeedbackApi(BaseModel):
model_config = {"populate_by_name": True}
user_agrees_with_judge: bool
input: str = Field(alias="input")
output: str
fails_specification: bool
user_feedback: str | None = None
class ClarifySpecApiInput(BaseModel):
target_task_prompt: str
task_input_schema: str
task_output_schema: str
spec_rendered_prompt_template: str
num_samples_per_topic: int
num_topics: int
providers: list[ModelProviderName]
num_exemplars: int = Field(default=10)
class RefineSpecApiInput(BaseModel):
target_task_info: TargetTaskInfoApi
spec: SpecInfoApi
examples_with_feedback: list[ExampleWithFeedbackApi]
class GenerateBatchApiInput(BaseModel):
target_task_prompt: str
task_input_schema: str
task_output_schema: str
spec_rendered_prompt_template: str
num_samples_per_topic: int
num_topics: int
class SubsampleBatchOutputItemApi(BaseModel):
input: str = Field(alias="input")
output: str
fails_specification: bool
class TaskMetadataApi(BaseModel):
model_name: str
model_provider_name: ModelProviderName
class PromptGenerationResultApi(BaseModel):
task_metadata: TaskMetadataApi
prompt: str
class ClarifySpecApiOutput(BaseModel):
examples_for_feedback: list[SubsampleBatchOutputItemApi]
judge_result: PromptGenerationResultApi
topic_generation_result: PromptGenerationResultApi
input_generation_result: PromptGenerationResultApi
class NewProposedSpecEditApi(BaseModel):
spec_field_name: str
proposed_edit: str
reason_for_edit: str
class RefineSpecApiOutput(BaseModel):
new_proposed_spec_edits: list[NewProposedSpecEditApi]
not_incorporated_feedback: str | None
class GenerateBatchApiOutput(BaseModel):
data_by_topic: dict[str, list[SampleApi]]
class CreateSpecWithCopilotRequest(BaseModel):
"""Request model for creating a spec with Kiln Copilot.
This endpoint uses Kiln Copilot to:
- Generate batch examples for eval, train, and golden datasets
- Create a judge eval config
- Create an eval with appropriate template/output scores
- Create and save the spec
If you don't want to use copilot, use the regular POST /spec endpoint instead.
The client is responsible for building:
- definition: The spec definition string (use buildSpecDefinition on client)
- properties: The spec properties object (filtered, with spec_type included)
"""
name: FilenameString
definition: str = Field(
description="The spec definition string, built by client using buildSpecDefinition()"
)
properties: SpecProperties = Field(
discriminator="spec_type",
description="The spec properties object, pre-built by client with spec_type included",
)
evaluate_full_trace: bool = False
reviewed_examples: list[ReviewedExample] = Field(default_factory=list)
judge_info: PromptGenerationResultApi
task_description: str = ""
task_prompt_with_few_shot: str = ""
async def _generate_copilot_examples(
task_prompt_with_few_shot: str,
task_input_schema: str,
task_output_schema: str,
spec_definition: str,
) -> list[SampleApi]:
"""Generate examples via the Kiln Copilot API.
Calls the copilot generate_batch endpoint and returns a flat list of SampleApi objects.
Raises HTTPException on API errors.
"""
api_key = _get_api_key()
client = get_authenticated_client(api_key)
generate_input = GenerateBatchInput.from_dict(
{
"target_task_prompt": task_prompt_with_few_shot,
"task_input_schema": task_input_schema,
"task_output_schema": task_output_schema,
"spec_rendered_prompt_template": spec_definition,
"num_samples_per_topic": NUM_SAMPLES_PER_TOPIC,
"num_topics": NUM_TOPICS,
}
)
result = await generate_batch_v1_copilot_generate_batch_post.asyncio(
client=client,
body=generate_input,
)
if result is None:
raise HTTPException(
status_code=500, detail="Failed to generate batch: No response"
)
if isinstance(result, HTTPValidationError):
raise HTTPException(
status_code=422,
detail=f"Validation error: {result.to_dict()}",
)
if not isinstance(result, GenerateBatchOutput):
raise HTTPException(
status_code=500,
detail=f"Failed to generate batch: Unexpected response type {type(result)}",
)
# Convert result to flat list of SampleApi
examples: list[SampleApi] = []
data_dict = result.to_dict().get("data_by_topic", {})
for topic_examples in data_dict.values():
for ex in topic_examples:
examples.append(
SampleApi(
input=ex.get("input", ""),
output=ex.get("output", ""),
)
)
return examples
def _get_api_key() -> str:
"""Get the Kiln Copilot API key from config, raising an error if not set."""
api_key = Config.shared().kiln_copilot_api_key
if not api_key:
raise HTTPException(
status_code=401,
detail="Kiln Copilot API key not configured. Please connect your API key in settings.",
)
return api_key
def connect_copilot_api(app: FastAPI):
@app.post("/api/copilot/clarify_spec")
async def clarify_spec(input: ClarifySpecApiInput) -> ClarifySpecApiOutput:
api_key = _get_api_key()
client = get_authenticated_client(api_key)
clarify_input = ClarifySpecInput.from_dict(input.model_dump())
result = await clarify_spec_v1_copilot_clarify_spec_post.asyncio(
client=client,
body=clarify_input,
)
if result is None:
raise HTTPException(
status_code=500, detail="Failed to clarify spec: No response"
)
if isinstance(result, HTTPValidationError):
raise HTTPException(
status_code=422,
detail=f"Validation error: {result.to_dict()}",
)
if isinstance(result, ClarifySpecOutput):
return ClarifySpecApiOutput.model_validate(result.to_dict())
raise HTTPException(
status_code=500,
detail=f"Failed to clarify spec: Unexpected response type {type(result)}",
)
@app.post("/api/copilot/refine_spec")
async def refine_spec(input: RefineSpecApiInput) -> RefineSpecApiOutput:
api_key = _get_api_key()
client = get_authenticated_client(api_key)
refine_input = RefineSpecInput.from_dict(input.model_dump())
result = await refine_spec_v1_copilot_refine_spec_post.asyncio(
client=client,
body=refine_input,
)
if result is None:
raise HTTPException(
status_code=500, detail="Failed to refine spec: No response"
)
if isinstance(result, HTTPValidationError):
raise HTTPException(
status_code=422,
detail=f"Validation error: {result.to_dict()}",
)
if isinstance(result, RefineSpecOutput):
return RefineSpecApiOutput.model_validate(result.to_dict())
raise HTTPException(
status_code=500,
detail=f"Failed to refine spec: Unexpected response type {type(result)}",
)
@app.post("/api/copilot/generate_batch")
async def generate_batch(input: GenerateBatchApiInput) -> GenerateBatchApiOutput:
api_key = _get_api_key()
client = get_authenticated_client(api_key)
generate_input = GenerateBatchInput.from_dict(input.model_dump())
result = await generate_batch_v1_copilot_generate_batch_post.asyncio(
client=client,
body=generate_input,
)
if result is None:
raise HTTPException(
status_code=500, detail="Failed to generate batch: No response"
)
if isinstance(result, HTTPValidationError):
raise HTTPException(
status_code=422,
detail=f"Validation error: {result.to_dict()}",
)
if isinstance(result, GenerateBatchOutput):
return GenerateBatchApiOutput.model_validate(result.to_dict())
raise HTTPException(
status_code=500,
detail=f"Failed to generate batch: Unexpected response type {type(result)}",
)
@app.post("/api/copilot/question_spec")
async def question_spec(
input: SpecQuestionerInput,
) -> QuestionSet:
api_key = _get_api_key()
client = get_authenticated_client(api_key)
questioner_input = SpecQuestionerInputServerApi.from_dict(input.model_dump())
result = await question_spec_v1_copilot_question_spec_post.asyncio(
client=client,
body=questioner_input,
)
if result is None:
raise HTTPException(
status_code=500, detail="Failed to generate questions: No response"
)
if isinstance(result, HTTPValidationError):
raise HTTPException(
status_code=422,
detail=f"Validation error: {result.to_dict()}",
)
if isinstance(result, QuestionSetServerApi):
return QuestionSet.model_validate(result.to_dict())
raise HTTPException(
status_code=500,
detail=f"Failed to generate questions: Unexpected response type {type(result)}",
)
@app.post("/api/copilot/refine_spec_with_question_answers")
async def submit_question_answers(
request: SubmitAnswersRequest,
) -> RefineSpecWithQuestionAnswersResponse:
api_key = _get_api_key()
client = get_authenticated_client(api_key)
submit_input = SubmitAnswersRequestServerApi.from_dict(request.model_dump())
result = await refine_spec_with_answers_v1_copilot_refine_spec_with_answers_post.asyncio(
client=client,
body=submit_input,
)
if result is None:
raise HTTPException(
status_code=500,
detail="Failed to refine spec with question answers: No response",
)
if isinstance(result, HTTPValidationError):
raise HTTPException(
status_code=422,
detail=f"Validation error: {result.to_dict()}",
)
if isinstance(result, RefineSpecWithQuestionAnswersResponseServerApi):
return RefineSpecWithQuestionAnswersResponse.model_validate(
result.to_dict()
)
raise HTTPException(
status_code=500,
detail=f"Failed to refine spec with question answers: Unexpected response type {type(result)}",
)
@app.post("/api/projects/{project_id}/tasks/{task_id}/spec_with_copilot")
async def create_spec_with_copilot(
project_id: str, task_id: str, request: CreateSpecWithCopilotRequest
) -> Spec:
"""Create a spec using Kiln Copilot.
This endpoint uses Kiln Copilot to create a spec with:
1. An eval for the spec with appropriate template
2. Batch examples via copilot API for eval, train, and golden datasets
3. A judge eval config (if judge_info provided)
4. The spec itself
If you don't need copilot, use POST /spec instead.
All models are validated before any saves occur. If validation fails,
no data is persisted.
"""
task = task_from_id(project_id, task_id)
# Generate tag suffixes
eval_tag_suffix = request.name.lower().replace(" ", "_")
eval_tag = f"eval_{eval_tag_suffix}"
train_tag = f"eval_train_{eval_tag_suffix}"
golden_tag = f"eval_golden_{eval_tag_suffix}"
# Extract spec_type from properties (discriminated union)
spec_type = request.properties["spec_type"]
# Determine eval properties
template = spec_eval_template(spec_type)
output_scores = [spec_eval_output_score(request.name)]
eval_set_filter_id = f"tag::{eval_tag}"
eval_configs_filter_id = f"tag::{golden_tag}"
evaluation_data_type = spec_eval_data_type(
spec_type, request.evaluate_full_trace
)
# Build models but don't save yet, collect all models first
models_to_save: list[Eval | EvalConfig | TaskRun | Spec] = []
# 1. Create the Eval
eval_model = Eval(
parent=task,
name=request.name,
description=None,
template=template,
output_scores=output_scores,
eval_set_filter_id=eval_set_filter_id,
eval_configs_filter_id=eval_configs_filter_id,
template_properties=None,
evaluation_data_type=evaluation_data_type,
)
models_to_save.append(eval_model)
# 2. Create judge eval config
eval_config = EvalConfig(
parent=eval_model,
name=generate_memorable_name(),
config_type=EvalConfigType.llm_as_judge,
model_name=request.judge_info.task_metadata.model_name,
model_provider=request.judge_info.task_metadata.model_provider_name,
properties={
"eval_steps": [request.judge_info.prompt],
"task_description": request.task_description,
},
)
models_to_save.append(eval_config)
# Set as default config after ID is assigned
eval_model.current_config_id = eval_config.id
# 3. Generate examples via copilot API
all_examples = await _generate_copilot_examples(
task_prompt_with_few_shot=request.task_prompt_with_few_shot,
task_input_schema=str(task.input_json_schema)
if task.input_json_schema
else "",
task_output_schema=str(task.output_json_schema)
if task.output_json_schema
else "",
spec_definition=request.definition,
)
# 4. Create TaskRuns for eval, train, and golden datasets
task_runs = create_dataset_task_runs(
all_examples=all_examples,
reviewed_examples=request.reviewed_examples,
eval_tag=eval_tag,
train_tag=train_tag,
golden_tag=golden_tag,
spec_name=request.name,
)
for run in task_runs:
run.parent = task
models_to_save.extend(task_runs)
# 5. Create the Spec using pre-computed definition and properties from client
spec = Spec(
parent=task,
name=request.name,
definition=request.definition,
properties=request.properties,
priority=Priority.p1,
status=SpecStatus.active,
tags=[],
eval_id=eval_model.id,
)
models_to_save.append(spec)
# All models are now created and validated via Pydantic.
# Save everything, with cleanup on failure.
saved_models: list[Eval | EvalConfig | TaskRun | Spec] = []
try:
eval_model.save_to_file()
saved_models.append(eval_model)
eval_config.save_to_file()
saved_models.append(eval_config)
for run in task_runs:
run.save_to_file()
saved_models.append(run)
spec.save_to_file()
saved_models.append(spec)
except Exception:
# Clean up any models that were successfully saved before the error
for model in reversed(saved_models):
try:
model.delete()
except Exception:
# Log cleanup error but continue, the original error is more important
logger.exception(
f"Failed to delete {type(model).__name__} during cleanup"
)
raise
return spec