SAP HANA Cloud Vector Engine으로 셀프 쿼리(Self Query)하기
SAP HANA Cloud Vector Engine으로 셀프 쿼리(Self Query)하기
SAP HANA 벡터 스토어 설정에 대한 자세한 내용은 벡터 스토어: SAP HANA 가이드를 참조하세요.
여기서도 동일한 설정을 사용합니다:
import * as dotenv from 'dotenv';
dotenv.config();
import hanaClient from "@sap/hana-client";
const connectionParams = {
host: process.env.HANA_DB_ADDRESS,
port: process.env.HANA_DB_PORT,
user: process.env.HANA_DB_USER,
password: process.env.HANA_DB_PASSWORD,
};
const client = hanaClient.createConnection(connectionParams);
// connect to hanaDB
await new Promise<void>((resolve, reject) => {
client.connect((err: Error) => {
// Use arrow function here
if (err) {
reject(err);
} else {
console.log("Connected to SAP HANA successfully.");
resolve();
}
});
});
좋은 성능으로 셀프 쿼리를 하기 위해 HANA의 벡터 스토어 테이블에 추가 메타데이터 필드를 만듭니다:
await new Promise<void>((resolve, reject) => {
client.exec(
`DROP TABLE LANGCHAIN_DEMO_SELF_QUERY`,
(dropErr: Error) => {
// Ignore drop errors
client.exec(
`CREATE TABLE "LANGCHAIN_DEMO_SELF_QUERY" (
"name" NVARCHAR(100), "is_active" BOOLEAN, "id" INTEGER, "height" DOUBLE,
"VEC_TEXT" NCLOB,
"VEC_META" NCLOB,
"VEC_VECTOR" REAL_VECTOR
)`,
(createErr: Error) => {
if (createErr) {
reject(createErr);
} else {
resolve();
}
}
);
}
);
});
몇 가지 문서를 추가해 보겠습니다.
import { HanaDB } from "@sap/hana-langchain";
import { Document } from "@langchain/core/documents";
import { OpenAIEmbeddings } from "@langchain/openai";
const embeddings = new OpenAIEmbeddings();
const db = new HanaDB(embeddings, {
connection: client,
tableName: "LANGCHAIN_DEMO_SELF_QUERY",
specificMetadataColumns: ["name", "is_active", "id", "height"],
});
await db.initialize();
const docs = [
new Document({
pageContent: "First",
metadata: { name: "adam", is_active: true, id: 1, height: 10.0 },
}),
new Document({
pageContent: "Second",
metadata: { name: "bob", is_active: false, id: 2, height: 5.7 },
}),
new Document({
pageContent: "Third",
metadata: { name: "jane", is_active: true, id: 3, height: 2.4 },
}),
];
await db.delete({ filter: {} });
await db.addDocuments(docs);
셀프 쿼리(Self querying)
이제 핵심입니다. HANA 벡터 스토어용 SelfQueryRetriever를 구성하는 방법은 다음과 같습니다:
import { ChatOpenAI } from "@langchain/openai";
import { AttributeInfo } from "@langchain/classic/chains/query_constructor";
import { SelfQueryRetriever } from "@langchain/classic/retrievers/self_query"
import { HanaTranslator } from "@sap/hana-langchain";
const llm = new ChatOpenAI({ model: "gpt-3.5-turbo" });
const metadataFieldInfo: AttributeInfo[] = [
{ name: "name", description: "The name of the person", type: "string" },
{ name: "is_active", description: "Whether the person is active", type: "boolean" },
{ name: "id", description: "The ID of the person", type: "integer" },
{ name: "height", description: "The height of the person", type: "float" },
];
const contentDescription = "A collection of persons";
const hanaTranslator = new HanaTranslator();
const retriever = await SelfQueryRetriever.fromLLM({
llm,
vectorStore: db,
documentContentDescription: contentDescription,
attributeInfo: metadataFieldInfo,
structuredQueryTranslator: hanaTranslator,
});
이 검색기를 사용해 사람에 대한 (셀프) 질의를 준비해 보겠습니다:
const queryPrompt = "Which person is not active?"
const retrievedDocs = await retriever.invoke(queryPrompt);
for (const doc of retrievedDocs){
console.log("-".repeat(80));
console.log(doc.pageContent + " " + JSON.stringify(doc.metadata));
}
--------------------------------------------------------------------------------
Second {"name":"bob","is_active":false,"id":2,"height":5.7}
출처: 문서
더 알아보기 (Learn more)
- 이 문서를 MCP로 연결하면 Claude, VSCode 등에서 실시간 답변을 받을 수 있어요.
- GitHub에서 이 페이지 편집하기 또는 이슈 제출하기.