Skip to content

Commit

Permalink
Merge pull request #68 from ks6088ts-labs/feature/issue-67_support-bi…
Browse files Browse the repository at this point in the history
…ng-search

support bing search
  • Loading branch information
ks6088ts authored Jun 1, 2024
2 parents 35b2174 + 985b3dd commit 21e4460
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
4 changes: 3 additions & 1 deletion backend/settings/azure_storage_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


class Settings(BaseSettings):
azure_storage_queue_connection_string: str = "<connection-string>"
azure_storage_queue_connection_string: str = (
"DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx/xxx;EndpointSuffix=core.windows.net"
)

model_config = SettingsConfigDict(
env_file="azure_storage_queue.env",
Expand Down
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
- [LangChain > Azure OpenAI](https://python.langchain.com/v0.1/docs/integrations/llms/azure_openai/)
- [Get started with LangSmith](https://docs.smith.langchain.com/)
- [Add message history (memory) > In-memory](https://python.langchain.com/v0.1/docs/expression_language/how_to/message_history/#in-memory)

### Tools

- [Tool calling agent](https://python.langchain.com/v0.1/docs/modules/agents/agent_types/tool_calling/)
- [LangChain > Tools > Bing Search](https://python.langchain.com/v0.1/docs/integrations/tools/bing_search/)
- [Tool Calling with LangChain](https://blog.langchain.dev/tool-calling-with-langchain/)
- [langchain/cookbook/tool_call_messages.ipynb](https://github.com/langchain-ai/langchain/blob/master/cookbook/tool_call_messages.ipynb?ref=blog.langchain.dev)
- [Azure OpenAI Service の ChatGPT に Bing Search や PDF 検索機能を LangGraph で接続しました。](https://techblog.cccmkhd.co.jp/entry/2024/04/09/173831)

## Backend

Expand Down
1 change: 1 addition & 0 deletions frontend.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ LANGCHAIN_API_KEY = "<your-api-key>"

BING_SUBSCRIPTION_KEY = "<your-api-key>"
BING_SEARCH_URL = "https://api.bing.microsoft.com/v7.0/search"
BING_SEARCH_K = 1
15 changes: 13 additions & 2 deletions frontend/solutions/agent_langgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
from langchain_openai import AzureChatOpenAI
from langgraph.graph import END, StateGraph

from frontend.solutions.internals.tools.examples import add, exponentiate, get_current_weather, multiply
from frontend.solutions.internals.tools.bing_search import bing_search_tool
from frontend.solutions.internals.tools.examples import (
add,
exponentiate,
get_date_diffs,
get_date_from_offset,
get_datetime_today,
multiply,
)

logger = logging.getLogger(__name__)
load_dotenv("frontend.env")
Expand All @@ -20,7 +28,10 @@
multiply,
exponentiate,
add,
get_current_weather,
get_date_diffs,
get_datetime_today,
get_date_from_offset,
bing_search_tool,
]
llm = AzureChatOpenAI(
api_key=getenv("AZURE_OPENAI_API_KEY"),
Expand Down
19 changes: 19 additions & 0 deletions frontend/solutions/internals/tools/bing_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from os import getenv

from langchain_core.tools import tool


@tool
def bing_search_tool(query: str) -> str:
"""Perform web searches by 'query' using Bing Search API. Returns search results as a string."""
from langchain_community.utilities import BingSearchAPIWrapper

return BingSearchAPIWrapper(
k=int(
getenv("BING_SEARCH_K", 1),
),
bing_search_url=getenv("BING_SEARCH_URL"),
bing_subscription_key=getenv("BING_SUBSCRIPTION_KEY"),
).run(
query=query,
)
26 changes: 23 additions & 3 deletions frontend/solutions/internals/tools/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ def add(x: float, y: float) -> float:


@tool
def get_current_weather(city: str) -> str:
"""Get the current weather in 'city'."""
return f"The weather in {city} is sunny."
def get_datetime_today() -> str:
"""Get today's date in the format 'YYYY/MM/DD'."""
import datetime

return datetime.datetime.now().strftime("%Y/%m/%d")


@tool
def get_date_from_offset(offset: int) -> str:
"""Get the date 'offset' days from today in the format 'YYYY/MM/DD'."""
import datetime

return (datetime.datetime.now() + datetime.timedelta(days=offset)).strftime("%Y/%m/%d")


@tool
def get_date_diffs(yyyymmdd1: str, yyyymmdd2: str) -> str:
"""Get the difference between two dates in days."""
import datetime

date1 = datetime.datetime.strptime(yyyymmdd1, "%Y/%m/%d")
date2 = datetime.datetime.strptime(yyyymmdd2, "%Y/%m/%d")
return abs((date1 - date2).days)

0 comments on commit 21e4460

Please sign in to comment.