-
Notifications
You must be signed in to change notification settings - Fork 0
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
impelment summarizer annotator #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
Add a 1-sentence text summary to each file or chunk node | ||
""" | ||
|
||
import asyncio | ||
from typing import Any, Coroutine | ||
|
||
from tqdm.asyncio import tqdm | ||
|
||
from ragdaemon.annotators.base_annotator import Annotator | ||
from ragdaemon.database import Database | ||
from ragdaemon.graph import KnowledgeGraph | ||
from ragdaemon.errors import RagdaemonError | ||
from spice import SpiceMessage | ||
|
||
summarizer_prompt = """\ | ||
Generate a 1-sentence summary of the provided code. Follow conventions of docstrings: | ||
write in the imerative voice and start with a verb. Do not include any preamble or | ||
asides. | ||
|
||
It may be useful to name specific fucntions from the target repository (not built-in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please correct the typo in 'fucntions' to 'functions'. |
||
Python functions) which are integral to the functioning of the target code. Include a | ||
maximum of two (2) such named functions, but err on the side of brevity. | ||
""" | ||
|
||
|
||
semaphore = asyncio.Semaphore(50) | ||
|
||
|
||
class Summarizer(Annotator): | ||
name = "summarizer" | ||
|
||
def is_complete(self, graph: KnowledgeGraph, db: Database) -> bool: | ||
return all( | ||
data.get("summary") is not None | ||
for _, data in graph.nodes(data=True) | ||
if data is not None and data.get("checksum") is not None | ||
) | ||
|
||
async def get_llm_response(self, document: str) -> str: | ||
if self.spice_client is None: | ||
raise RagdaemonError("Spice client is not initialized.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
global semaphore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of |
||
async with semaphore: | ||
messages: list[SpiceMessage] = [ | ||
{"role": "system", "content": summarizer_prompt}, | ||
{"role": "user", "content": document}, | ||
] | ||
response = await self.spice_client.get_response( | ||
messages=messages, | ||
) | ||
return response.text | ||
|
||
async def get_summary(self, data: dict[str, Any], db: Database): | ||
"""Asynchronously generate summary and update graph and db""" | ||
record = db.get(data["checksum"]) | ||
document = record["documents"][0] | ||
metadatas = record["metadatas"][0] | ||
summary = await self.get_llm_response(document) | ||
metadatas["summary"] = summary | ||
db.update(data["checksum"], metadatas=metadatas) | ||
data["summary"] = summary | ||
|
||
async def annotate( | ||
self, graph: KnowledgeGraph, db: Database, refresh: bool = False | ||
) -> KnowledgeGraph: | ||
# Generate/add summaries to nodes with checksums (file, chunk, diff) | ||
tasks = [] | ||
for _, data in graph.nodes(data=True): | ||
if data is None or data.get("checksum") is None: | ||
continue | ||
if data.get("summary") is not None and not refresh: | ||
continue | ||
tasks.append(self.get_summary(data, db)) | ||
if len(tasks) > 0: | ||
if self.verbose: | ||
await tqdm.gather(*tasks, desc="Summarizing code...") | ||
else: | ||
await asyncio.gather(*tasks) | ||
return graph |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from unittest.mock import AsyncMock, patch | ||
|
||
import pytest | ||
|
||
from ragdaemon.annotators import Summarizer | ||
from ragdaemon.daemon import Daemon | ||
|
||
|
||
@pytest.fixture | ||
def mock_get_llm_response(): | ||
with patch( | ||
"ragdaemon.annotators.summarizer.Summarizer.get_llm_response", | ||
return_value="summary of", | ||
) as mock: | ||
yield mock | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_summarizer_annotate(cwd, mock_get_llm_response): | ||
daemon = Daemon( | ||
cwd=cwd, | ||
annotators={"hierarchy": {}}, | ||
) | ||
await daemon.update(refresh=True) | ||
summarizer = Summarizer(spice_client=AsyncMock()) | ||
actual = await summarizer.annotate(daemon.graph, daemon.db) | ||
for _, data in actual.nodes(data=True): | ||
if data.get("checksum") is not None: | ||
assert data.get("summary") == "summary of" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be a typo here. The word 'imerative' should be corrected to 'imperative' to maintain professionalism in the documentation.