|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from pydantic import BaseModel |
| 4 | + |
| 5 | +from workflowai.core.client.models import TaskRunResponse |
| 6 | +from workflowai.core.domain.task import Task |
| 7 | + |
| 8 | + |
| 9 | +class GenerateChangelogFromPropertiesTaskInput(BaseModel): |
| 10 | + temperature: Optional[float] = None |
| 11 | + |
| 12 | + |
| 13 | +class GenerateChangelogFromPropertiesTaskOutput(BaseModel): |
| 14 | + changes: Optional[list[str]] = None |
| 15 | + |
| 16 | + |
| 17 | +def test_task_run_model_validate(): |
| 18 | + json_str = """{ |
| 19 | + "id": "e3683ced-efd2-4b15-9ab4-aefdf17c4c19", |
| 20 | + "task_id": "generate-changelog-from-properties", |
| 21 | + "task_schema_id": 1, |
| 22 | + "task_input": { |
| 23 | + "temperature": 1 |
| 24 | + }, |
| 25 | + "task_input_hash": "6faebdc55135f380b00e7ada53f5cccc", |
| 26 | + "task_input_preview": "old_task_group: {properties: {temperature: 1, instructions: \\"Add 5 to the n", |
| 27 | + "task_output": { |
| 28 | + "changes": [ |
| 29 | + "Temperature decreased from Creative to 0.73" |
| 30 | + ] |
| 31 | + }, |
| 32 | + "task_output_hash": "d057c1f26fd8447c11cda7300e0f3717", |
| 33 | + "task_output_preview": "changes: [\\"Temperature decreased from Creative to 0.73\\"]", |
| 34 | + "group": { |
| 35 | + "id": "68eead780d01791ff2e09d39055ae6e8", |
| 36 | + "iteration": 36, |
| 37 | + "properties": { |
| 38 | + "model": "gemini-1.5-flash-002", |
| 39 | + "provider": "google", |
| 40 | + "temperature": 0, |
| 41 | + "instructions": "", |
| 42 | + "max_tokens": null, |
| 43 | + "runner_name": "WorkflowAI", |
| 44 | + "runner_version": "v0.1.0", |
| 45 | + "few_shot": null, |
| 46 | + "template_name": "v1", |
| 47 | + "task_variant_id": "fa546275ed8f6c801d6c6f174828d615" |
| 48 | + }, |
| 49 | + "tags": [ |
| 50 | + "model=gemini-1.5-flash-002", |
| 51 | + "provider=google", |
| 52 | + "temperature=0" |
| 53 | + ], |
| 54 | + "aliases": null, |
| 55 | + "is_external": null, |
| 56 | + "is_favorite": null, |
| 57 | + "notes": null, |
| 58 | + "similarity_hash": "", |
| 59 | + "benchmark_for_datasets": null |
| 60 | + }, |
| 61 | + "status": "success", |
| 62 | + "error": null, |
| 63 | + "start_time": "2024-10-01T17:55:06.241000Z", |
| 64 | + "end_time": "2024-10-01T17:55:07.879000Z", |
| 65 | + "duration_seconds": 1.638103, |
| 66 | + "cost_usd": 0.00004651875, |
| 67 | + "created_at": "2024-10-01T17:55:07.879000Z", |
| 68 | + "updated_at": "2024-10-01T17:55:07.879000Z", |
| 69 | + "example_id": null, |
| 70 | + "corrections": null, |
| 71 | + "parent_task_ids": null, |
| 72 | + "scores": null, |
| 73 | + "labels": null, |
| 74 | + "metadata": { |
| 75 | + "used_alias": "environment=production" |
| 76 | + }, |
| 77 | + "llm_completions": [ |
| 78 | + { |
| 79 | + "messages": [ |
| 80 | + { |
| 81 | + "role": "system", |
| 82 | + "content": "" |
| 83 | + }, |
| 84 | + { |
| 85 | + "role": "user", |
| 86 | + "content": "" |
| 87 | + } |
| 88 | + ], |
| 89 | + "response": "{\\"changes\\": [\\"Temperature decreased from Creative to 0.73\\"]}", |
| 90 | + "usage": { |
| 91 | + "completion_token_count": 13.5, |
| 92 | + "completion_cost_usd": 0.00000405, |
| 93 | + "prompt_token_count": 566.25, |
| 94 | + "prompt_cost_usd": 0.00004246875 |
| 95 | + } |
| 96 | + } |
| 97 | + ], |
| 98 | + "config_id": null, |
| 99 | + "dataset_benchmark_ids": null, |
| 100 | + "is_free": null, |
| 101 | + "author_tenant": null |
| 102 | +}""" |
| 103 | + |
| 104 | + task_run = TaskRunResponse.model_validate_json(json_str).to_domain( |
| 105 | + Task( |
| 106 | + input_class=GenerateChangelogFromPropertiesTaskInput, |
| 107 | + output_class=GenerateChangelogFromPropertiesTaskOutput, |
| 108 | + ), |
| 109 | + ) |
| 110 | + assert task_run.id == "e3683ced-efd2-4b15-9ab4-aefdf17c4c19" |
0 commit comments