|
|
|
|
|
|
|
|
from typing import Dict, Any, List, Optional |
|
|
from .base_tool import BaseTool |
|
|
|
|
|
class CitationTool(BaseTool): |
|
|
"""Tool for finding and formatting scientific citations. |
|
|
|
|
|
This tool helps agents find appropriate citations for claims and format them |
|
|
according to standard scientific citation styles. |
|
|
""" |
|
|
|
|
|
def __init__(self): |
|
|
"""Initialize the citation tool.""" |
|
|
super().__init__( |
|
|
name="citation", |
|
|
description="Find and format scientific citations" |
|
|
) |
|
|
|
|
|
def execute( |
|
|
self, |
|
|
query: str, |
|
|
citation_style: str = "apa", |
|
|
max_results: int = 5, |
|
|
min_year: Optional[int] = None |
|
|
) -> List[Dict[str, Any]]: |
|
|
"""Find and format citations for a given query. |
|
|
|
|
|
Args: |
|
|
query: The search query for finding relevant citations |
|
|
citation_style: Citation style to use (apa, mla, chicago, etc.) |
|
|
max_results: Maximum number of citation results to return |
|
|
min_year: Optional minimum publication year to filter results |
|
|
|
|
|
Returns: |
|
|
A list of formatted citations and their metadata |
|
|
""" |
|
|
self.logger.info(f"Finding citations for: {query} (style: {citation_style}, max: {max_results})") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current_year = 2023 |
|
|
start_year = min_year or (current_year - 10) |
|
|
|
|
|
mock_citations = [ |
|
|
{ |
|
|
"authors": ["Smith, J.", "Johnson, K."], |
|
|
"year": start_year + i, |
|
|
"title": f"Research on {query}: A Comprehensive Analysis", |
|
|
"journal": "Journal of Scientific Research", |
|
|
"volume": "42", |
|
|
"issue": "3", |
|
|
"pages": f"{100 + i*10}-{110 + i*10}", |
|
|
"doi": f"10.1234/jsr.{2023}.{1000 + i}", |
|
|
"relevance_score": 0.95 - (i * 0.1), |
|
|
"formatted_citation": f"Smith, J., & Johnson, K. ({start_year + i}). Research on {query}: A Comprehensive Analysis. Journal of Scientific Research, 42(3), {100 + i*10}-{110 + i*10}. https://doi.org/10.1234/jsr.{2023}.{1000 + i}" |
|
|
} |
|
|
for i in range(min(max_results, 10)) |
|
|
] |
|
|
|
|
|
return mock_citations |
|
|
|
|
|
def format_citation(self, citation_data: Dict[str, Any], style: str) -> str: |
|
|
"""Format citation data according to a specific style. |
|
|
|
|
|
Args: |
|
|
citation_data: Dictionary with citation data |
|
|
style: Citation style to use |
|
|
|
|
|
Returns: |
|
|
Formatted citation string |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if style.lower() == "apa": |
|
|
|
|
|
authors = ", ".join(citation_data.get("authors", [])) |
|
|
year = citation_data.get("year", "n.d.") |
|
|
title = citation_data.get("title", "") |
|
|
journal = citation_data.get("journal", "") |
|
|
volume = citation_data.get("volume", "") |
|
|
issue = citation_data.get("issue", "") |
|
|
pages = citation_data.get("pages", "") |
|
|
doi = citation_data.get("doi", "") |
|
|
|
|
|
return f"{authors} ({year}). {title}. {journal}, {volume}({issue}), {pages}. https://doi.org/{doi}" |
|
|
|
|
|
elif style.lower() == "mla": |
|
|
|
|
|
authors = ", ".join(citation_data.get("authors", [])) |
|
|
title = citation_data.get("title", "") |
|
|
journal = citation_data.get("journal", "") |
|
|
volume = citation_data.get("volume", "") |
|
|
issue = citation_data.get("issue", "") |
|
|
year = citation_data.get("year", "n.d.") |
|
|
pages = citation_data.get("pages", "") |
|
|
|
|
|
return f"{authors}. \"{title}.\" {journal}, vol. {volume}, no. {issue}, {year}, pp. {pages}." |
|
|
|
|
|
else: |
|
|
|
|
|
authors = ", ".join(citation_data.get("authors", [])) |
|
|
year = citation_data.get("year", "n.d.") |
|
|
title = citation_data.get("title", "") |
|
|
journal = citation_data.get("journal", "") |
|
|
|
|
|
return f"{authors} ({year}). {title}. {journal}." |
|
|
|
|
|
def get_parameters_schema(self) -> Dict[str, Any]: |
|
|
"""Get the JSON schema for the tool's parameters. |
|
|
|
|
|
Returns: |
|
|
A dictionary containing the JSON schema |
|
|
""" |
|
|
return { |
|
|
"type": "object", |
|
|
"properties": { |
|
|
"query": { |
|
|
"type": "string", |
|
|
"description": "The search query for finding relevant citations" |
|
|
}, |
|
|
"citation_style": { |
|
|
"type": "string", |
|
|
"description": "Citation style to use", |
|
|
"enum": ["apa", "mla", "chicago", "harvard", "ieee"], |
|
|
"default": "apa" |
|
|
}, |
|
|
"max_results": { |
|
|
"type": "integer", |
|
|
"description": "Maximum number of citation results to return", |
|
|
"default": 5 |
|
|
}, |
|
|
"min_year": { |
|
|
"type": "integer", |
|
|
"description": "Optional minimum publication year to filter results" |
|
|
} |
|
|
}, |
|
|
"required": ["query"] |
|
|
} |
|
|
|