I need the agent to query different collections or databases. The vector database I’m using is Milvus. However, I noticed that when building Milvus, only one collection can be specified and the database is always the default “default”. So I customized the knowledge_retriever and selected the database and collection, and everything worked well. But I hope the database name and collection name can be dynamically passed in through the agent. I haven’t found a proper way to receive any parameters passed when calling the Post /agents/{agent_id}/runs interface in the customized knowledge_retriever. When I try to print **kwargs, it’s always empty. Here is my code:
def search_for_jfr(
agent: Agent,
query: str,
num_documents: Optional[int] = 5,
**kwargs) → Optional[list[dict]] :
print(f"kwargs:{kwargs}")
connect_milvus()
db.using_database(“default”)
coll = Collection("jfr_goods")
coll.load()
vec = ollama_embed(\[query\])\[0\]
q = l2_normalize(vec)
res = coll.search(
data=\[q\],
anns_field="text_vector",
param={"metric_type": "COSINE", "params": {"nprobe": 16}},
limit=num_documents,
output_fields=\["\*"\]
)
results = \[\]
for hit in res\[0\]:
content = {
"productID": hit.entity.get("id"),
"productName": hit.entity.get("goods_name"),
"price": f"¥{hit.entity.get('price')}",
"store": hit.entity.get("store_name"),
"status": hit.entity.get("market_enable"),
"inventory": hit.entity.get("quantity"),
"productType": hit.entity.get("goods_type"),
"sales_volume": hit.entity.get("buy_count"),
"originalImage": hit.entity.get("original_image"),
"whether_it_is_self_operated": "yse" if hit.entity.get("self_operated") else "no",
"similarityScore": round(float(hit.distance), 4)
}
results.append(content)
print(f"format_the_search_results:{results}")
return results
def get_jfr_agent():
jfr_product = Milvus(
uri = f"http://{config.MILVUS_HOST}:{config.MILVUS_PORT}",
collection = “jfr_goods”,
embedder = embedder
)
jfr_product_knowledge = Knowledge(vector_db=jfr_product)
assistant = Agent(
name = "jfr_assistant",
role="answer_the_questions_based_on_the_knowledge_base",
model = OpenAILike(
id = "qwen-plus",
api_key = "sk-7ff2d2d2ee5b41a99c59e381c72aa75e",
base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1",
),
knowledge_retriever=search_for_jfr,
add_history_to_context = True,
db = pg_db,
add_knowledge_to_context = True,
knowledge = jfr_product_knowledge,
markdown = True
)
return assistant