ElGraph.LLM.Structured (ElGraph v0.3.0)

Copy Markdown View Source

스키마 검증 + 오류 피드백 재시도로 LLM에서 구조화 출력을 얻는다 (신뢰성 패턴).

Instructor / Pydantic AI의 핵심 루프를 ElGraph LLM 추상화 위에 올린 것:

  1. 대화를 LLM에 보내 응답(JSON)을 받는다.
  2. JSON을 디코드해 NimbleOptions 스키마로 검증한다(코드펜스로 감싸여 오면 벗긴다).
  3. 실패하면 검증 오류를 메시지로 되먹여 재시도한다(:max_retries, 기본 2).
  4. 통과하면 {:ok, %{data: 검증된_맵, usage: 누적_usage}}.
schema = [name: [type: :string, required: true], age: [type: :pos_integer, required: true]]
{:ok, %{data: %{name: "Ada", age: 36}}} =
  ElGraph.LLM.Structured.generate({MyLLM, cfg}, [ElGraph.LLM.user("a person")], schema)

스키마는 ElGraph.Guardrail.validate_schema/1와 동일한 NimbleOptions keyword 형식이다. 검증을 끝까지 통과 못 하면 {:error, {:invalid_output, NimbleOptions.ValidationError | reason}}, LLM 호출 자체가 실패하면 그 {:error, term}을 그대로 전파한다.

Summary

Functions

generate(llm, messages, schema, opts \\ [])

@spec generate({module(), term()}, [ElGraph.LLM.message()], keyword(), keyword()) ::
  {:ok, %{data: map(), usage: ElGraph.LLM.usage()}} | {:error, term()}