Skip to content

Implement support to BatchAPIs to gather evidence #687

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

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2385dce
Implements OpenAIBatchLLMModel
maykcaldas Nov 14, 2024
8a21055
Incorporates OpenAIBatchLLMModel to get_evidence
maykcaldas Nov 14, 2024
5f59681
Merge branch 'main' into batch_api
maykcaldas Nov 15, 2024
6f7bbb5
Merge branch 'main' into batch_api
maykcaldas Nov 16, 2024
e8dc0d0
Started anthropic batch api support implementation
maykcaldas Nov 15, 2024
899de43
Removed the skip_system argument from the new classes and tests to ma…
maykcaldas Nov 15, 2024
16c3988
Switched to async OpenAI client
maykcaldas Nov 16, 2024
d10a268
Added logging to the batch processing
maykcaldas Nov 16, 2024
0fe9aa1
Created mock server to test openAI batch API
maykcaldas Nov 18, 2024
a9ad540
Implemented batch support to Anthropic
maykcaldas Nov 18, 2024
9a0a6c4
Merge branch 'main' into batch_api
maykcaldas Nov 18, 2024
723650d
Updated uv.lock to include imports for the batch API
maykcaldas Nov 18, 2024
660bfa0
Implements tests with a mocked server for anthropic
maykcaldas Nov 18, 2024
977a025
Added type hints to satisfy the pre-commit
maykcaldas Nov 19, 2024
ee351f2
Merge branch 'main' into batch_api
maykcaldas Nov 19, 2024
293658a
Updates uv on github actions to include extra requirements
maykcaldas Nov 19, 2024
1ad1c7c
Removed the --all-extras flag from uv in github workflow
maykcaldas Nov 19, 2024
af32005
Refactored OpenAiBatchStatus and AnthropicBatchStatus to make the cod…
maykcaldas Nov 19, 2024
63e4b39
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] Nov 19, 2024
f61e629
Merge branch 'main' into batch_api
maykcaldas Nov 19, 2024
d7dbd72
Cleaned unneeded comments
maykcaldas Nov 19, 2024
7c37f6d
Updated the way the system message is passed to anthropic
maykcaldas Nov 19, 2024
de18907
changed how the file is passed to openai
maykcaldas Nov 20, 2024
3e72bd4
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] Nov 20, 2024
7c7f4b8
Avoided writing to a file when sending the batch to openAi
maykcaldas Nov 20, 2024
6c8f186
Skipped writing a file. Instead, the content is directly passed to th…
maykcaldas Nov 20, 2024
0e43a7c
Merge branch 'main' into batch_api
maykcaldas Nov 20, 2024
17c26eb
Fixed lint error
maykcaldas Nov 20, 2024
c258306
Updated the batch time limit settings name
maykcaldas Nov 20, 2024
4b8e1c3
Removed type hints from docstrings in gather_with_batch
maykcaldas Nov 20, 2024
8b5c1fa
Added exception in map_fxn_summary to treat multiple reponses
maykcaldas Nov 20, 2024
ab40b54
Added a description explaining the llm_type attribute
maykcaldas Nov 20, 2024
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
4 changes: 4 additions & 0 deletions paperqa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
HybridEmbeddingModel,
LiteLLMEmbeddingModel,
LiteLLMModel,
OpenAIBatchLLMModel,
AnthropicBatchLLMModel,
LLMModel,
LLMResult,
NumpyVectorStore,
Expand All @@ -38,6 +40,8 @@
"LLMResult",
"LiteLLMEmbeddingModel",
"LiteLLMModel",
"OpenAIBatchLLMModel",
"AnthropicBatchLLMModel",
"NumpyVectorStore",
"PQASession",
"QueryRequest",
Expand Down
47 changes: 47 additions & 0 deletions paperqa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,50 @@ async def map_fxn_summary(
),
llm_result,
)

async def gather_with_batch(
matches: list[Text],
question: str,
prompt_runner: PromptRunner | None,
extra_prompt_data: dict[str, str] | None = None,
parser: Callable[[str], dict[str, Any]] | None = None,
callbacks: list[Callable[[str], None]] | None = None,
) -> list[tuple[Context, LLMResult]]:
"""Gathers a batch of results for a given text."""
data = [
{"question": question,
"citation": m.name + ": " + m.doc.formatted_citation,
"text": m.text} |
extra_prompt_data or {}
for m in matches
]

llm_results = await prompt_runner(
data,
callbacks,
)

results_data = []
scores = []
for r in llm_results:
try:
results_data.append(parser(r.text))
scores.append(r.pop("relevance_score"))
# just in case question was present
r.pop("question", None)
except:
results_data.append({})
scores.append(extract_score(r.text))

return [
(
Context(
context=strip_citations(llm_result.text),
text=m,
model_extra={},
score=score,
**r,
),
llm_result,
) for r, m, llm_result, score in zip(results_data, matches, llm_results, scores)
]
55 changes: 37 additions & 18 deletions paperqa/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
)

from paperqa.clients import DEFAULT_CLIENTS, DocMetadataClient
from paperqa.core import llm_parse_json, map_fxn_summary
from paperqa.core import (
llm_parse_json,
map_fxn_summary,
gather_with_batch
)
from paperqa.llms import (
EmbeddingModel,
LLMModel,
Expand All @@ -40,6 +44,7 @@
LLMResult,
PQASession,
Text,
Context,
set_llm_session_ids,
)
from paperqa.utils import (
Expand All @@ -50,6 +55,8 @@
maybe_is_text,
md5sum,
name_in_text,
extract_score,
strip_citations
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -600,23 +607,35 @@ async def aget_evidence(
)

with set_llm_session_ids(session.id):
results = await gather_with_concurrency(
answer_config.max_concurrent_requests,
[
map_fxn_summary(
text=m,
question=session.question,
prompt_runner=prompt_runner,
extra_prompt_data={
"summary_length": answer_config.evidence_summary_length,
"citation": f"{m.name}: {m.doc.formatted_citation}",
},
parser=llm_parse_json if prompt_config.use_json else None,
callbacks=callbacks,
)
for m in matches
],
)
if evidence_settings.use_batch_in_summary:
results = await gather_with_batch(
matches = matches,
question = session.question,
prompt_runner=prompt_runner,
extra_prompt_data={
"summary_length": answer_config.evidence_summary_length,
},
parser=llm_parse_json if prompt_config.use_json else None,
callbacks=callbacks,
)
else:
results = await gather_with_concurrency(
answer_config.max_concurrent_requests,
[
map_fxn_summary(
text=m,
question=session.question,
prompt_runner=prompt_runner,
extra_prompt_data={
"summary_length": answer_config.evidence_summary_length,
"citation": f"{m.name}: {m.doc.formatted_citation}",
},
parser=llm_parse_json if prompt_config.use_json else None,
callbacks=callbacks,
)
for m in matches
],
)

for _, llm_result in results:
session.add_tokens(llm_result)
Expand Down
Loading
Loading