Skip to content

⚡️ Speed up method GroqModel._process_response by 44% #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: try-refinement
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions pydantic_ai_slim/pydantic_ai/_thinking_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ def split_content_into_text_and_thinking(content: str) -> list[ThinkingPart | Te
something else, we just match the tag to make it easier for other models that don't support the `ThinkingPart`.
"""
parts: list[ThinkingPart | TextPart] = []

start_index = content.find(START_THINK_TAG)
while start_index >= 0:
before_think, content = content[:start_index], content[start_index + len(START_THINK_TAG) :]
if before_think:
parts.append(TextPart(content=before_think))
end_index = content.find(END_THINK_TAG)
if end_index >= 0:
think_content, content = content[:end_index], content[end_index + len(END_THINK_TAG) :]
parts.append(ThinkingPart(content=think_content))
else:
# We lose the `<think>` tag, but it shouldn't matter.
parts.append(TextPart(content=content))
content = ''
start_index = content.find(START_THINK_TAG)
if content:
parts.append(TextPart(content=content))

start_index = 0
while True:
start = content.find('<think>', start_index)
if start < 0:
if start_index < len(content):
parts.append(TextPart(content=content[start_index:]))
break
if start > start_index:
parts.append(TextPart(content=content[start_index:start]))
think_start = start + 7 # len('<think>')
end = content.find('</think>', think_start)
if end < 0:
parts.append(TextPart(content=content[think_start:]))
break
if end > think_start:
parts.append(ThinkingPart(content=content[think_start:end]))
start_index = end + 8 # len('</think>')
return parts
Loading