Skip to content

Commit 45ad063

Browse files
committed
add code samples
1 parent fe30514 commit 45ad063

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

scripts/generate_openapi.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ def extract_default_value(content: str) -> Any:
226226

227227
# If it's an array or object, try to find the complete value
228228
if value_str.startswith("[") or value_str.startswith("{"):
229-
# Find the complete array/object by looking in the original content
230229
# Find the position of the opening bracket in the original content
231230
default_start = content.find("default:")
232231
if default_start != -1:
@@ -669,6 +668,18 @@ def generate_openapi_schema(evaluators: Dict[str, Any]) -> Dict[str, Any]:
669668
},
670669
},
671670
},
671+
"x-codeSamples": [
672+
{
673+
"lang": "python",
674+
"label": "Python",
675+
"source": f'import langwatch\n@langwatch.span()\ndef llm_step():\n ... # your existing code\n result = langwatch.get_current_span().evaluate(\n "{evaluator_id}",\n{chr(10).join(f" {field}=\"\", # required" for field in evaluator.get("requiredFields", []))}\n{chr(10).join(f" {field}=\"\", # optional" for field in evaluator.get("optionalFields", []))}\n settings={{}},\n )\n print(result)',
676+
},
677+
{
678+
"lang": "typescript",
679+
"label": "TypeScript",
680+
"source": f'import {{ type LangWatchTrace }} from "langwatch";\n\nasync function llmStep({{ message, trace }}: {{ message: string, trace: LangWatchTrace }}): Promise<string> {{\n const span = trace.startLLMSpan({{ name: "llmStep" }});\n \n // ... your existing code\n\n // call the evaluator either on a span or on a trace\n const result = await span.evaluate({{\n evaluator: "{evaluator_id}",\n name: "",\n{chr(10).join(f" {field}: \"\", // required" for field in evaluator.get("requiredFields", []))}\n{chr(10).join(f" {field}: \"\", # optional" for field in evaluator.get("optionalFields", []))} settings: {{}},\n }})\n\n console.log(result);',
681+
},
682+
],
672683
}
673684
}
674685
else:
@@ -725,6 +736,18 @@ def generate_openapi_schema(evaluators: Dict[str, Any]) -> Dict[str, Any]:
725736
},
726737
},
727738
},
739+
"x-codeSamples": [
740+
{
741+
"lang": "python",
742+
"label": "Python",
743+
"source": f'import langwatch\n@langwatch.span()\ndef llm_step():\n ... # your existing code\n result = langwatch.get_current_span().evaluate(\n "{evaluator_id}",\n{chr(10).join(f" {field}=\"\", # required" for field in evaluator.get("requiredFields", []))}\n{chr(10).join(f" {field}=\"\", # optional" for field in evaluator.get("optionalFields", []))}\n settings={{}},\n )\n print(result)',
744+
},
745+
{
746+
"lang": "typescript",
747+
"label": "TypeScript",
748+
"source": f'import {{ type LangWatchTrace }} from "langwatch";\n\nasync function llmStep({{ message, trace }}: {{ message: string, trace: LangWatchTrace }}): Promise<string> {{\n const span = trace.startLLMSpan({{ name: "llmStep" }});\n \n // ... your existing code\n\n // call the evaluator either on a span or on a trace\n const result = await span.evaluate({{\n evaluator: "{evaluator_id}",\n name: "",\n{chr(10).join(f" {field}: \"\", // required" for field in evaluator.get("requiredFields", []))}\n{chr(10).join(f" {field}: \"\", # optional" for field in evaluator.get("optionalFields", []))} settings: {{}},\n }})\n\n console.log(result);',
749+
},
750+
],
728751
}
729752
}
730753

0 commit comments

Comments
 (0)