|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from app.desktop.studio_server.api_client.kiln_ai_server_client.api.copilot import ( |
| 4 | + clarify_spec_v1_copilot_clarify_spec_post, |
| 5 | + generate_batch_v1_copilot_generate_batch_post, |
| 6 | + refine_spec_v1_copilot_refine_spec_post, |
| 7 | +) |
| 8 | +from app.desktop.studio_server.api_client.kiln_ai_server_client.models import ( |
| 9 | + ClarifySpecInput, |
| 10 | + ClarifySpecOutput, |
| 11 | + ExampleWithFeedback, |
| 12 | + GenerateBatchInput, |
| 13 | + GenerateBatchOutput, |
| 14 | + HTTPValidationError, |
| 15 | + RefineSpecInput, |
| 16 | + RefineSpecOutput, |
| 17 | + SpecInfo, |
| 18 | + TaskInfo, |
| 19 | +) |
| 20 | +from app.desktop.studio_server.api_client.kiln_server_client import ( |
| 21 | + get_authenticated_client, |
| 22 | +) |
| 23 | +from fastapi import FastAPI, HTTPException |
| 24 | +from kiln_ai.utils.config import Config |
| 25 | +from pydantic import BaseModel, Field |
| 26 | + |
| 27 | + |
| 28 | +class ClarifySpecApiInput(BaseModel): |
| 29 | + task_prompt_with_few_shot: str |
| 30 | + task_input_schema: str |
| 31 | + task_output_schema: str |
| 32 | + spec_rendered_prompt_template: str |
| 33 | + num_samples_per_topic: int |
| 34 | + num_topics: int |
| 35 | + num_exemplars: int = Field(default=10) |
| 36 | + |
| 37 | + |
| 38 | +class RefineSpecApiInput(BaseModel): |
| 39 | + task_prompt_with_few_shot: str |
| 40 | + task_input_schema: str |
| 41 | + task_output_schema: str |
| 42 | + task_info: dict[str, Any] |
| 43 | + spec: dict[str, Any] |
| 44 | + examples_with_feedback: list[dict[str, Any]] |
| 45 | + |
| 46 | + |
| 47 | +class GenerateBatchApiInput(BaseModel): |
| 48 | + task_prompt_with_few_shot: str |
| 49 | + task_input_schema: str |
| 50 | + task_output_schema: str |
| 51 | + spec_rendered_prompt_template: str |
| 52 | + num_samples_per_topic: int |
| 53 | + num_topics: int |
| 54 | + enable_scoring: bool = Field(default=False) |
| 55 | + |
| 56 | + |
| 57 | +def _get_api_key() -> str: |
| 58 | + """Get the Kiln Copilot API key from config, raising an error if not set.""" |
| 59 | + api_key = Config.shared().kiln_copilot_api_key |
| 60 | + if not api_key: |
| 61 | + raise HTTPException( |
| 62 | + status_code=401, |
| 63 | + detail="Kiln Copilot API key not configured. Please connect your API key in settings.", |
| 64 | + ) |
| 65 | + return api_key |
| 66 | + |
| 67 | + |
| 68 | +def connect_copilot_api(app: FastAPI): |
| 69 | + @app.post("/api/copilot/clarify_spec") |
| 70 | + async def clarify_spec(input: ClarifySpecApiInput) -> dict[str, Any]: |
| 71 | + api_key = _get_api_key() |
| 72 | + client = get_authenticated_client(api_key) |
| 73 | + |
| 74 | + clarify_input = ClarifySpecInput( |
| 75 | + task_prompt_with_few_shot=input.task_prompt_with_few_shot, |
| 76 | + task_input_schema=input.task_input_schema, |
| 77 | + task_output_schema=input.task_output_schema, |
| 78 | + spec_rendered_prompt_template=input.spec_rendered_prompt_template, |
| 79 | + num_samples_per_topic=input.num_samples_per_topic, |
| 80 | + num_topics=input.num_topics, |
| 81 | + num_exemplars=input.num_exemplars, |
| 82 | + ) |
| 83 | + |
| 84 | + result = await clarify_spec_v1_copilot_clarify_spec_post.asyncio( |
| 85 | + client=client, |
| 86 | + body=clarify_input, |
| 87 | + ) |
| 88 | + |
| 89 | + if result is None: |
| 90 | + raise HTTPException( |
| 91 | + status_code=500, detail="Failed to clarify spec: No response" |
| 92 | + ) |
| 93 | + |
| 94 | + if isinstance(result, HTTPValidationError): |
| 95 | + raise HTTPException( |
| 96 | + status_code=422, |
| 97 | + detail=f"Validation error: {result.to_dict()}", |
| 98 | + ) |
| 99 | + |
| 100 | + if isinstance(result, ClarifySpecOutput): |
| 101 | + return result.to_dict() |
| 102 | + |
| 103 | + raise HTTPException( |
| 104 | + status_code=500, |
| 105 | + detail=f"Failed to clarify spec: Unexpected response type {type(result)}", |
| 106 | + ) |
| 107 | + |
| 108 | + @app.post("/api/copilot/refine_spec") |
| 109 | + async def refine_spec(input: RefineSpecApiInput) -> dict[str, Any]: |
| 110 | + api_key = _get_api_key() |
| 111 | + client = get_authenticated_client(api_key) |
| 112 | + |
| 113 | + task_info = TaskInfo.from_dict(input.task_info) |
| 114 | + spec = SpecInfo.from_dict(input.spec) |
| 115 | + examples_with_feedback = [ |
| 116 | + ExampleWithFeedback.from_dict(ex) for ex in input.examples_with_feedback |
| 117 | + ] |
| 118 | + |
| 119 | + refine_input = RefineSpecInput( |
| 120 | + task_prompt_with_few_shot=input.task_prompt_with_few_shot, |
| 121 | + task_input_schema=input.task_input_schema, |
| 122 | + task_output_schema=input.task_output_schema, |
| 123 | + task_info=task_info, |
| 124 | + spec=spec, |
| 125 | + examples_with_feedback=examples_with_feedback, |
| 126 | + ) |
| 127 | + |
| 128 | + result = await refine_spec_v1_copilot_refine_spec_post.asyncio( |
| 129 | + client=client, |
| 130 | + body=refine_input, |
| 131 | + ) |
| 132 | + |
| 133 | + if result is None: |
| 134 | + raise HTTPException( |
| 135 | + status_code=500, detail="Failed to refine spec: No response" |
| 136 | + ) |
| 137 | + |
| 138 | + if isinstance(result, HTTPValidationError): |
| 139 | + raise HTTPException( |
| 140 | + status_code=422, |
| 141 | + detail=f"Validation error: {result.to_dict()}", |
| 142 | + ) |
| 143 | + |
| 144 | + if isinstance(result, RefineSpecOutput): |
| 145 | + return result.to_dict() |
| 146 | + |
| 147 | + raise HTTPException( |
| 148 | + status_code=500, |
| 149 | + detail=f"Failed to refine spec: Unexpected response type {type(result)}", |
| 150 | + ) |
| 151 | + |
| 152 | + @app.post("/api/copilot/generate_batch") |
| 153 | + async def generate_batch(input: GenerateBatchApiInput) -> dict[str, Any]: |
| 154 | + api_key = _get_api_key() |
| 155 | + client = get_authenticated_client(api_key) |
| 156 | + |
| 157 | + generate_input = GenerateBatchInput( |
| 158 | + task_prompt_with_few_shot=input.task_prompt_with_few_shot, |
| 159 | + task_input_schema=input.task_input_schema, |
| 160 | + task_output_schema=input.task_output_schema, |
| 161 | + spec_rendered_prompt_template=input.spec_rendered_prompt_template, |
| 162 | + num_samples_per_topic=input.num_samples_per_topic, |
| 163 | + num_topics=input.num_topics, |
| 164 | + enable_scoring=input.enable_scoring, |
| 165 | + ) |
| 166 | + |
| 167 | + result = await generate_batch_v1_copilot_generate_batch_post.asyncio( |
| 168 | + client=client, |
| 169 | + body=generate_input, |
| 170 | + ) |
| 171 | + |
| 172 | + if result is None: |
| 173 | + raise HTTPException( |
| 174 | + status_code=500, detail="Failed to generate batch: No response" |
| 175 | + ) |
| 176 | + |
| 177 | + if isinstance(result, HTTPValidationError): |
| 178 | + raise HTTPException( |
| 179 | + status_code=422, |
| 180 | + detail=f"Validation error: {result.to_dict()}", |
| 181 | + ) |
| 182 | + |
| 183 | + if isinstance(result, GenerateBatchOutput): |
| 184 | + return result.to_dict() |
| 185 | + |
| 186 | + raise HTTPException( |
| 187 | + status_code=500, |
| 188 | + detail=f"Failed to generate batch: Unexpected response type {type(result)}", |
| 189 | + ) |
0 commit comments