Replies: 1 comment 2 replies
-
For my use case, I managed to get something working doing the following: def generate_structured_completion(
self, system_prompt: str, messages: Sequence[ChatMessage], response_model: type[T]
) -> T:
client = Anthropic(api_key=api_key)
# This is for all other single model cases
# stolen from instructor/patch.py
if get_origin(response_model) is Iterable:
iterable_element_class = get_args(response_model)[0]
response_model = IterableModel(iterable_element_class) # type: ignore
if not issubclass(response_model, OpenAISchema):
response_model = openai_schema(response_model)
response: PromptCachingBetaMessage = cast(
PromptCachingBetaMessage,
client.beta.prompt_caching.messages.create(
system_prompt,
messages,
extra_headers={"anthropic-beta": "prompt-caching-2024-07-31"},
tools=[response_model.anthropic_schema],
),
)
tool_calls = [c.input for c in response.content if c.type == "tool_use"]
if tool_calls:
tool_call = tool_call[0]
return response_model.model_validate(tool_call)
else:
# no tools were called. Might be helpful for other, so leaving it here. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I think it would be a great fit for instructor to support Anthropic's new Prompt Caching. E.g. for parsing structural data, it could store the base prompt once and only receive the content-to-parse in subsequent calls to save tokens.
Beta Was this translation helpful? Give feedback.
All reactions