File size: 7,484 Bytes
9433533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from typing import Any

from pydantic import BaseModel, Field
from langchain_core.output_parsers.pydantic import PydanticOutputParser
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.runnables import RunnableSequence
from langchain_core.prompts import PromptTemplate
from langchain_core.tools import tool, BaseTool

from thefuzz import fuzz

from ask_candid.tools.utils import format_candid_profile_link
from ask_candid.base.api_base import BaseAPI
from ask_candid.base.config.rest import CANDID_SEARCH_API


class OrganizationNames(BaseModel):
    """List of names of social-sector organizations, such as nonprofits and foundations."""
    orgnames: list[str] = Field(..., description="List of organization names.")


class OrganizationIdentifierArgs(BaseModel):
    text: str = Field(..., description="Chat model response text which contains named organizations.")


class OrganizationIdentifier(BaseTool):
    llm: BaseChatModel
    parser: type[PydanticOutputParser] = PydanticOutputParser(pydantic_object=OrganizationNames)
    template: str = """Extract only the names of officially recognized organizations, foundations, and government
    entities from the text below. Do not include any entries that contain descriptions, regional identifiers, or
    explanations within parentheses or following the name. Strictly exclude databases, resources, crowdfunding
    platforms, and general terms. Provide the output only in the specified JSON format.

    input text: ```{chatbot_output}```
    output format: ```{format_instructions}```
    """

    name: str = "organization-identifier"
    description: str = """
    Identify the names of nonprofits and foundations from chat model responses. If it is likely that a response contains
    proper names then it should be processed through this tool.

    Examples
    --------
    >>> `organization_identifier('My Favorite Foundation awarded a grant to My Favorite Nonprofit.')`
    >>> `organization_identifier('The LoremIpsum Nonprofit will be running a community event this Thursday')`
    """
    args_schema: type[OrganizationIdentifierArgs] = OrganizationIdentifierArgs

    def _build_pipeline(self):
        prompt = PromptTemplate(
            template=self.template,
            input_variables=["chatbot_output"],
            partial_variables={"format_instructions": self.parser.get_format_instructions()}
        )
        return RunnableSequence(prompt, self.llm, self.parser)

    def _run(self, text: str) -> str:
        chain = self._build_pipeline()
        result: OrganizationNames = chain.invoke({"chatbot_output": text})
        return result.orgnames

    async def _arun(self, text: str) -> str:
        chain = self._build_pipeline()
        result: OrganizationNames = await chain.ainvoke({"chatbot_output": text})
        return result.orgnames


def name_search(name: str) -> list[dict[str, Any]]:
    candid_org_search = BaseAPI(
        url=f'{CANDID_SEARCH_API["url"]}/v1/search',
        headers={"x-api-key": CANDID_SEARCH_API["key"]}
    )
    results = candid_org_search.get(
        query=f"'{name}'",
        searchMode="organization_only",
        rowCount=5
    )
    return results.get("returnedOrgs") or []


def find_similar(name: str, potential_matches: list[dict[str, Any]], threshold: int = 80):
    for org in potential_matches:
        similarity = max(
            fuzz.ratio(name.lower(), (org["orgName"] or "").lower()),
            fuzz.ratio(name.lower(), (org["akaName"] or "").lower()),
            fuzz.ratio(name.lower(), (org["dbaName"] or "").lower()),
        )
        if similarity >= threshold:
            yield org, similarity


@tool(response_format="content_and_artifact")
def find_mentioned_organizations(organizations: list[str]) -> tuple[str, dict[str, str]]:
    """Match organization names found in a chat response to official organizations tracked by Candid. This involves
    using the Candid Search API in a lookup mode, and then finding the best result(s) using a heuristic string
    similarity search.

    This tool is focused on getting links to the organization's Candid profile for the user to click and explore in
    more detail.

    Use the URLs here to replace organization names in the chat response with links to the organization's profile. Links
    to Candid profiles **MUST** be used to do the following:
    1. Generate direct links to Candid organization profiles
    2. Provide a mechanism for users to easily access detailed organizational information
    3. Enhance responses with authoritative source links

    Key Usage Requirements:
    - Always incorporate returned profile URLs directly into the response text
    - Replace organization name mentions with hyperlinked Candid profile URLs
    - Prioritize creating a seamless user experience by making URLs contextually relevant

    Example Desired Output:
    Instead of: 'The Gates Foundation does impressive work.'
    Use: 'The [Gates Foundation](https://app.candid.org/profile/XXXXX) does impressive work.'

    The function returns a tuple with:
    - A link information text (optional)
    - A dictionary mapping input names to their best Candid Search profile URL

    Failure to integrate the URLs into the response is considered an incomplete implementation.",

    Examples
    --------
    >>> find_mentioned_organizations(organizations=['Gates Foundation', 'Candid'])

    Parameters
    ----------
    organizations : list[str]
        A list of organization name strings found in a chat response message which need to be matches

    Returns
    -------
    tuple[str, dict[str, str]]
        (Link information text, mapping input name --> Candid Search profile URL of the best potential match)
    """

    output = {}
    for name in organizations:
        search_results = name_search(name)
        try:
            best_result, _ = max(find_similar(name=name, potential_matches=search_results), key=lambda x: x[-1])
        except ValueError:
            # no similar organizations could be found for this one, keep going
            continue
        output[name] = format_candid_profile_link(best_result["candidEntityID"])

    response = [f"The Candid profile link for {name} is {url}" for name, url in output.items()]
    return '. '.join(response), output


@tool
def find_mentioned_organizations_detailed(organizations: list[str]) -> dict[str, dict[str, Any]]:
    """Match organization names found in a chat response to official organizations tracked by Candid. This involves
    using the Candid Search API in a lookup mode, and then finding the best result(s) using a heuristic string
    similarity search.

    Examples
    --------
    >>> find_mentioned_organizations(organizations=['Gates Foundation', 'Candid'])

    Parameters
    ----------
    organizations : list[str]
        A list of organization name strings found in a chat response message which need to be matches

    Returns
    -------
    dict[str, dict[str, Any]]
        Mapping from the input name(s) to the best potential match.
    """

    output = {}
    for name in organizations:
        search_results = name_search(name)
        try:
            best_result, _ = max(find_similar(name=name, potential_matches=search_results), key=lambda x: x[-1])
        except ValueError:
            # no similar organizations could be found for this one, keep going
            continue
        output[name] = best_result
    return output