Skip to content

Commit

Permalink
Generate JSON output via tool call
Browse files Browse the repository at this point in the history
  • Loading branch information
chaecramb committed Jan 30, 2025
1 parent e1bd956 commit f1cc88e
Showing 1 changed file with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ def initialize(context)
def call
start_time = Clock.monotonic_time

response = bedrock_client.converse(system: [{ text: system_prompt }], model_id: BEDROCK_MODEL, messages:, inference_config:)
response = bedrock_client.converse(
system: [{ text: system_prompt }],
model_id: BEDROCK_MODEL,
messages:,
inference_config:,
tool_config:,
)

context.answer.assign_llm_response("structured_answer", response.to_h)
message = response["output"]["message"]["content"][0]["text"]
message = response["output"]["message"]["content"][0]["tool_use"]["input"]["answer"]
context.answer.assign_attributes(message:, status: "answered")
context.answer.assign_metrics("structured_answer", build_metrics(start_time, response))
end
Expand Down Expand Up @@ -57,5 +63,37 @@ def build_metrics(start_time, response)
llm_completion_tokens: response.dig("usage", "output_tokens"),
}
end

def tool_config
{
tools: tools,
tool_choice: {
tool: {
name: "answer_confidence",
},
},
}
end

def tools
[
{
tool_spec: {
name: "answer_confidence",
description: "Prints the answer of a given question with a confidence score.",
input_schema: {
json: {
type: "object",
properties: {
answer: { description: "Your answer to the question in markdown format", title: "Answer", type: "string" },
confidence: { description: "Your confidence in the answer provided, ranging from 0.0 to 1.0", title: "Answered", type: "number" },
},
required: %w[answer confidence],
},
},
},
},
]
end
end
end

0 comments on commit f1cc88e

Please sign in to comment.