Skip to content

Commit

Permalink
fix: Add parse_from_string method to BatchJob (#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwilsonmg authored Oct 7, 2024
1 parent d968970 commit aab61f5
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions instructor/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ def parse_from_file(

return res, error_objs

@classmethod
def parse_from_string(
cls, content: str, response_model: type[T]
) -> tuple[list[T], list[dict[Any, Any]]]:
res: list[T] = []
error_objs: list[dict[Any, Any]] = []
lines = content.splitlines()
for line in lines:
data = json.loads(line)
try:
res.append(
response_model(
**json.loads(
data["response"]["body"]["choices"][0]["message"][
"tool_calls"
][0]["function"]["arguments"]
)
)
)
except Exception:
error_objs.append(data)

return res, error_objs

@classmethod
def create_from_messages(
cls,
Expand Down

0 comments on commit aab61f5

Please sign in to comment.