Skip to content

Commit 27e6860

Browse files
committed
add contoso rules tool
1 parent 275deed commit 27e6860

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

frontend/.env.sample

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ AZURE_OPENAI_API_KEY = "<aoai-api-key>"
77
AZURE_OPENAI_API_VERSION = "2024-05-01-preview"
88
AZURE_OPENAI_MODEL_WHISPER = "whisper"
99
AZURE_OPENAI_MODEL_CHAT = "gpt-4o"
10+
AZURE_OPENAI_MODEL_EMBEDDING = "text-embedding-3-large"
11+
12+
# Azure AI Search
13+
AZURE_AI_SEARCH_ENDPOINT = "https://<your-aisearch-name>.search.windows.net"
14+
AZURE_AI_SEARCH_API_KEY = "<api-key>"
1015

1116
# LangSmith
1217
LANGCHAIN_TRACING_V2 = "false" # set to "true" to enable tracing

frontend/pages/tool_agent.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
1010
from langchain_core.runnables import RunnableConfig
1111
from langchain_openai import AzureChatOpenAI
12+
from tools.fetch_contoso_rules import fetch_contoso_rules
1213
from tools.search_ddg import search_ddg
1314

1415
logger = logging.getLogger(__name__)
@@ -64,7 +65,10 @@ def init_messages():
6465

6566

6667
def create_agent():
67-
tools = [search_ddg]
68+
tools = [
69+
search_ddg,
70+
fetch_contoso_rules,
71+
]
6872
prompt = ChatPromptTemplate.from_messages(
6973
[
7074
("system", CUSTOM_SYSTEM_PROMPT),
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# GitHub: https://github.com/naotaka1128/llm_app_codes/chapter_010/tools/fetch_qa_content.py
2+
3+
from os import getenv
4+
5+
from langchain_community.vectorstores.azuresearch import AzureSearch
6+
from langchain_core.pydantic_v1 import BaseModel, Field
7+
from langchain_core.tools import tool
8+
from langchain_openai import AzureOpenAIEmbeddings
9+
10+
11+
class FetchContentInput(BaseModel):
12+
"""型を指定するためのクラス"""
13+
14+
query: str = Field()
15+
16+
17+
def get_embeddings():
18+
return AzureOpenAIEmbeddings(
19+
api_key=getenv("AZURE_OPENAI_API_KEY"),
20+
api_version=getenv("AZURE_OPENAI_API_VERSION"),
21+
azure_endpoint=getenv("AZURE_OPENAI_ENDPOINT"),
22+
azure_deployment=getenv("AZURE_OPENAI_MODEL_EMBEDDING"),
23+
)
24+
25+
26+
def create_azure_search(index_name: str) -> AzureSearch:
27+
return AzureSearch(
28+
azure_search_endpoint=getenv("AZURE_AI_SEARCH_ENDPOINT"),
29+
azure_search_key=getenv("AZURE_AI_SEARCH_API_KEY"),
30+
index_name=index_name,
31+
embedding_function=get_embeddings().embed_query,
32+
additional_search_client_options={"retry_total": 4},
33+
)
34+
35+
36+
@tool(args_schema=FetchContentInput)
37+
def fetch_contoso_rules(query):
38+
"""
39+
Contoso 社の就業規則情報から、関連するコンテンツを見つけるツールです。
40+
Contoso 社に関する具体的な知識を得るのに役立ちます。
41+
42+
このツールは `similarity`(類似度)と `content`(コンテンツ)を返します。
43+
- 'similarity'は、回答が質問にどの程度関連しているかを示します。
44+
値が高いほど、質問との関連性が高いことを意味します。
45+
'similarity'値が0.5未満のドキュメントは返されません。
46+
- 'content'は、質問に対する回答のテキストを提供します。
47+
通常、よくある質問とその対応する回答で構成されています。
48+
49+
空のリストが返された場合、ユーザーの質問に対する回答が見つからなかったことを意味します。
50+
その場合、ユーザーに質問内容を明確にしてもらうのが良いでしょう。
51+
52+
Returns
53+
-------
54+
List[Dict[str, Any]]:
55+
- page_content
56+
- similarity: float
57+
- content: str
58+
"""
59+
db = create_azure_search("contoso_rules")
60+
docs = db.similarity_search_with_relevance_scores(
61+
query=query,
62+
k=3,
63+
score_threshold=0.5,
64+
)
65+
return [
66+
{
67+
"similarity": similarity,
68+
"content": i.page_content,
69+
}
70+
for i, similarity in docs
71+
]
72+
73+
74+
if __name__ == "__main__":
75+
import logging
76+
77+
from dotenv import load_dotenv
78+
79+
logging.basicConfig(
80+
format="[%(asctime)s] %(levelname)7s from %(name)s in %(pathname)s:%(lineno)d: " "%(message)s",
81+
level=logging.DEBUG,
82+
force=True,
83+
)
84+
85+
load_dotenv()
86+
docs = fetch_contoso_rules("ドレスコード")
87+
for doc in docs:
88+
print(doc)

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ langchain-community = "^0.2.10"
6262
youtube-transcript-api = "^0.6.2"
6363
pytube = "^15.0.0"
6464
duckduckgo-search = "^6.2.3"
65+
azure-search-documents = "^11.5.0"
6566

6667

6768
[tool.poetry.group.azure-functions.dependencies]

0 commit comments

Comments
 (0)