-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
langchain[minor]: Add StackExchange API integration (#14002)
Implements [#12115](#12115) Who can review? @baskaryan , @eyurtsev , @hwchase17 Integrated Stack Exchange API into Langchain, enabling access to diverse communities within the platform. This addition enhances Langchain's capabilities by allowing users to query Stack Exchange for specialized information and engage in discussions. The integration provides seamless interaction with Stack Exchange content, offering content from varied knowledge repositories. A notebook example and test cases were included to demonstrate the functionality and reliability of this integration. - Add StackExchange as a tool. - Add unit test for the StackExchange wrapper and tool. - Add documentation for the StackExchange wrapper and tool. If you have time, could you please review the code and provide any feedback as necessary! My team is welcome to any suggestions. --------- Co-authored-by: Yuval Kamani <yuvalkamani@gmail.com> Co-authored-by: Aryan Thakur <aryanthakur@Aryans-MacBook-Pro.local> Co-authored-by: Manas1818 <79381912+manas1818@users.noreply.github.com> Co-authored-by: aryan-thakur <61063777+aryan-thakur@users.noreply.github.com> Co-authored-by: Bagatur <baskaryan@gmail.com>
- Loading branch information
1 parent
d4405bc
commit 7ec4dbe
Showing
12 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Stack Exchange | ||
|
||
>[Stack Exchange](https://en.wikipedia.org/wiki/Stack_Exchange) is a network of | ||
question-and-answer (Q&A) websites on topics in diverse fields, each site covering | ||
a specific topic, where questions, answers, and users are subject to a reputation award process. | ||
|
||
This page covers how to use the `Stack Exchange API` within LangChain. | ||
|
||
## Installation and Setup | ||
- Install requirements with | ||
```bash | ||
pip install stackapi | ||
``` | ||
|
||
## Wrappers | ||
|
||
### Utility | ||
|
||
There exists a StackExchangeAPIWrapper utility which wraps this API. To import this utility: | ||
|
||
```python | ||
from langchain.utilities import StackExchangeAPIWrapper | ||
``` | ||
|
||
For a more detailed walkthrough of this wrapper, see [this notebook](/docs/integrations/tools/stackexchange). | ||
|
||
### Tool | ||
|
||
You can also easily load this wrapper as a Tool (to use with an Agent). | ||
You can do this with: | ||
```python | ||
from langchain.agents import load_tools | ||
tools = load_tools(["stackexchange"]) | ||
``` | ||
|
||
For more information on tools, see [this page](/docs/modules/agents/tools/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# StackExchange\n", | ||
"\n", | ||
"This notebook goes over how to use the stack exchange component.\n", | ||
"\n", | ||
"All you need to do is install stackapi:\n", | ||
"1. pip install stackapi\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"pip install stackapi" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain.utilities import StackExchangeAPIWrapper" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"stackexchange = StackExchangeAPIWrapper()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"stackexchange.run(\"zsh: command not found: python\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""StackExchange API toolkit.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Tool for the Wikipedia API.""" | ||
|
||
from typing import Optional | ||
|
||
from langchain.callbacks.manager import CallbackManagerForToolRun | ||
from langchain.tools.base import BaseTool | ||
from langchain.utilities.stackexchange import StackExchangeAPIWrapper | ||
|
||
|
||
class StackExchangeTool(BaseTool): | ||
"""Tool that uses StackExchange""" | ||
|
||
name: str = "StackExchange" | ||
description: str = ( | ||
"A wrapper around StackExchange. " | ||
"Useful for when you need to answer specific programming questions" | ||
"code excerpts, code examples and solutions" | ||
"Input should be a fully formed question." | ||
) | ||
api_wrapper: StackExchangeAPIWrapper | ||
|
||
def _run( | ||
self, | ||
query: str, | ||
run_manager: Optional[CallbackManagerForToolRun] = None, | ||
) -> str: | ||
"""Use the Stack Exchange tool.""" | ||
return self.api_wrapper.run(query) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import html | ||
from typing import Any, Dict, Literal | ||
|
||
from langchain.pydantic_v1 import BaseModel, Field, root_validator | ||
|
||
|
||
class StackExchangeAPIWrapper(BaseModel): | ||
"""Wrapper for Stack Exchange API.""" | ||
|
||
client: Any #: :meta private: | ||
max_results: int = 3 | ||
"""Max number of results to include in output.""" | ||
query_type: Literal["all", "title", "body"] = "all" | ||
"""Which part of StackOverflows items to match against. One of 'all', 'title', | ||
'body'. Defaults to 'all'. | ||
""" | ||
fetch_params: Dict[str, Any] = Field(default_factory=dict) | ||
"""Additional params to pass to StackApi.fetch.""" | ||
result_separator: str = "\n\n" | ||
"""Separator between question,answer pairs.""" | ||
|
||
@root_validator() | ||
def validate_environment(cls, values: Dict) -> Dict: | ||
"""Validate that the required Python package exists.""" | ||
try: | ||
from stackapi import StackAPI | ||
|
||
values["client"] = StackAPI("stackoverflow") | ||
except ImportError: | ||
raise ImportError( | ||
"The 'stackapi' Python package is not installed. " | ||
"Please install it with `pip install stackapi`." | ||
) | ||
return values | ||
|
||
def run(self, query: str) -> str: | ||
"""Run query through StackExchange API and parse results.""" | ||
|
||
query_key = "q" if self.query_type == "all" else self.query_type | ||
output = self.client.fetch( | ||
"search/excerpts", **{query_key: query}, **self.fetch_params | ||
) | ||
if len(output["items"]) < 1: | ||
return f"No relevant results found for '{query}' on Stack Overflow." | ||
questions = [ | ||
item for item in output["items"] if item["item_type"] == "question" | ||
][: self.max_results] | ||
answers = [item for item in output["items"] if item["item_type"] == "answer"] | ||
results = [] | ||
for question in questions: | ||
res_text = f"Question: {question['title']}\n{question['excerpt']}" | ||
relevant_answers = [ | ||
answer | ||
for answer in answers | ||
if answer["question_id"] == question["question_id"] | ||
] | ||
accepted_answers = [ | ||
answer for answer in relevant_answers if answer["is_accepted"] | ||
] | ||
if relevant_answers: | ||
top_answer = ( | ||
accepted_answers[0] if accepted_answers else relevant_answers[0] | ||
) | ||
excerpt = html.unescape(top_answer["excerpt"]) | ||
res_text += f"\nAnswer: {excerpt}" | ||
results.append(res_text) | ||
|
||
return self.result_separator.join(results) |
23 changes: 23 additions & 0 deletions
23
libs/langchain/tests/integration_tests/utilities/test_stackexchange.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Integration test for Stack Exchange.""" | ||
from langchain.utilities import StackExchangeAPIWrapper | ||
|
||
|
||
def test_call() -> None: | ||
"""Test that call runs.""" | ||
stackexchange = StackExchangeAPIWrapper() | ||
output = stackexchange.run("zsh: command not found: python") | ||
assert output != "hello" | ||
|
||
|
||
def test_failure() -> None: | ||
"""Test that call that doesn't run.""" | ||
stackexchange = StackExchangeAPIWrapper() | ||
output = stackexchange.run("sjefbsmnf") | ||
assert output == "No relevant results found for 'sjefbsmnf' on Stack Overflow" | ||
|
||
|
||
def test_success() -> None: | ||
"""Test that call that doesn't run.""" | ||
stackexchange = StackExchangeAPIWrapper() | ||
output = stackexchange.run("zsh: command not found: python") | ||
assert "zsh: command not found: python" in output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters