1
1
from functools import lru_cache
2
2
3
+ from langchain .tools import tool
4
+ from pydantic import BaseModel , Field
3
5
from pydantic_settings import BaseSettings , SettingsConfigDict
4
6
from qdrant_client import QdrantClient
5
7
from qdrant_client .http .models import UpdateResult
6
8
from qdrant_client .models import Distance , PointStruct , VectorParams
7
9
10
+ from template_langgraph .llms .azure_openais import AzureOpenAiWrapper
11
+
8
12
9
13
class Settings (BaseSettings ):
10
14
qdrant_url : str = "http://localhost:6333"
@@ -48,6 +52,16 @@ def create_collection(
48
52
)
49
53
return result
50
54
55
+ def delete_collection (
56
+ self ,
57
+ collection_name : str ,
58
+ ) -> bool :
59
+ """Delete a collection in Qdrant."""
60
+ if self .client .collection_exists (collection_name = collection_name ):
61
+ self .client .delete_collection (collection_name = collection_name )
62
+ return True
63
+ return False
64
+
51
65
def upsert_points (
52
66
self ,
53
67
collection_name : str ,
@@ -59,3 +73,50 @@ def upsert_points(
59
73
points = points ,
60
74
wait = True ,
61
75
)
76
+
77
+ def query_points (
78
+ self ,
79
+ collection_name : str ,
80
+ query : list [float ],
81
+ limit : int = 3 ,
82
+ ) -> list [PointStruct ]:
83
+ """Query points from a Qdrant collection."""
84
+ return self .client .query_points (
85
+ collection_name = collection_name ,
86
+ query = query ,
87
+ limit = limit ,
88
+ ).points
89
+
90
+
91
+ class QdrantInput (BaseModel ):
92
+ keywords : str = Field (description = "Keywords to search" )
93
+
94
+
95
+ class QdrantOutput (BaseModel ):
96
+ file_name : str = Field (description = "The file name" )
97
+ content : str = Field (description = "The content of the file" )
98
+
99
+
100
+ @tool (args_schema = QdrantInput )
101
+ def search_qdrant (
102
+ keywords : str ,
103
+ ) -> list [QdrantOutput ]:
104
+ """
105
+ 空想上のシステム「KABUTO」の過去のシステムのトラブルシュート事例が蓄積されたデータベースから、関連する情報を取得します。
106
+ """
107
+ wrapper = QdrantClientWrapper ()
108
+ query_vector = AzureOpenAiWrapper ().create_embedding (keywords )
109
+ results = wrapper .query_points (
110
+ collection_name = "qa_kabuto" ,
111
+ query = query_vector ,
112
+ limit = 3 ,
113
+ )
114
+ outputs = []
115
+ for result in results :
116
+ outputs .append (
117
+ QdrantOutput (
118
+ file_name = result .payload ["file_name" ],
119
+ content = result .payload ["content" ],
120
+ ),
121
+ )
122
+ return outputs
0 commit comments