Skip to content

Commit

Permalink
feat (sdk): add readonly ChromaDB to Python SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
karahan-sahin committed May 1, 2024
1 parent 2a99ec5 commit 873e675
Show file tree
Hide file tree
Showing 14 changed files with 480 additions and 516 deletions.
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"fetch": "^1.1.0",
"global": "^4.4.0",
"http-errors": "~1.6.3",
"http-proxy-middleware": "^3.0.0",
"jade": "^1.11.0",
"joi": "^17.8.3",
"lodash": "^4.17.21",
Expand Down
19 changes: 18 additions & 1 deletion api/src/packages/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import RedisClient from "../clients/redis.js";

import * as Sentry from "@sentry/node";
import { ProfilingIntegration } from "@sentry/profiling-node";
import { createProxyMiddleware } from 'http-proxy-middleware';

const app = express();

Expand Down Expand Up @@ -69,7 +70,7 @@ import {
isStreamID,
} from "../types/validators.js";

app.use(express.json());
app.use(/^\/(?!chroma).*/, express.json());

const validator = ejv.createValidator({
passError: true,
Expand All @@ -78,6 +79,22 @@ const validator = ejv.createValidator({
// Authenticate
app.use(authenticateMiddleware);

const simpleRequestLogger = (proxyServer, options) => {
proxyServer.on('proxyReq', (proxyReq, req, res) => {
console.log(`[HPM] [${req.method}] ${req.url}`); // outputs: [HPM] GET /users
});
};

// Chroma Proxy
app.use(
'/chroma',
createProxyMiddleware({
target: process.env.CHROMA_API_URL,
changeOrigin: true,
plugins: [simpleRequestLogger],
}),
);

// DIDs
app.get(
"/dids/:did/indexes/:type?",
Expand Down
45 changes: 44 additions & 1 deletion api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3242,6 +3242,13 @@
resolved "https://registry.yarnpkg.com/@types/eventsource/-/eventsource-1.1.15.tgz#949383d3482e20557cbecbf3b038368d94b6be27"
integrity sha512-XQmGcbnxUNa06HR3VBVkc9+A2Vpi9ZyLJcdS5dwaQQ/4ZMWFO+5c90FnMUpbtMZwB/FChoYHwuVg8TvkECacTA==

"@types/http-proxy@^1.17.10":
version "1.17.14"
resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.14.tgz#57f8ccaa1c1c3780644f8a94f9c6b5000b5e2eec"
integrity sha512-SSrD0c1OQzlFX7pGu1eXxSEjemej64aaNPRhhVYUGqXh0BtldAAx37MG8btcumvpgKyZp1F5Gn3JkktdxiFv6w==
dependencies:
"@types/node" "*"

"@types/minimatch@^3.0.4":
version "3.0.5"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
Expand Down Expand Up @@ -5379,6 +5386,11 @@ ethjs-util@^0.1.6:
is-hex-prefixed "1.0.0"
strip-hex-prefix "1.0.0"

eventemitter3@^4.0.0:
version "4.0.7"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==

eventemitter3@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-5.0.1.tgz#53f5ffd0a492ac800721bb42c66b841de96423c4"
Expand Down Expand Up @@ -5575,7 +5587,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/fn.name/-/fn.name-1.1.0.tgz#26cad8017967aea8731bc42961d04a3d5988accc"
integrity sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==

follow-redirects@^1.14.0, follow-redirects@^1.14.4, follow-redirects@^1.15.6:
follow-redirects@^1.0.0, follow-redirects@^1.14.0, follow-redirects@^1.14.4, follow-redirects@^1.15.6:
version "1.15.6"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==
Expand Down Expand Up @@ -5934,6 +5946,27 @@ http-proxy-agent@^4.0.1:
agent-base "6"
debug "4"

http-proxy-middleware@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-3.0.0.tgz#550790357d6f92a9b82ab2d63e07343a791cf26b"
integrity sha512-36AV1fIaI2cWRzHo+rbcxhe3M3jUDCNzc4D5zRl57sEWRAxdXYtw7FSQKYY6PDKssiAKjLYypbssHk+xs/kMXw==
dependencies:
"@types/http-proxy" "^1.17.10"
debug "^4.3.4"
http-proxy "^1.18.1"
is-glob "^4.0.1"
is-plain-obj "^3.0.0"
micromatch "^4.0.5"

http-proxy@^1.18.1:
version "1.18.1"
resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.18.1.tgz#401541f0534884bbf95260334e72f88ee3976549"
integrity sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==
dependencies:
eventemitter3 "^4.0.0"
follow-redirects "^1.0.0"
requires-port "^1.0.0"

http-shutdown@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/http-shutdown/-/http-shutdown-1.2.2.tgz#41bc78fc767637c4c95179bc492f312c0ae64c5f"
Expand Down Expand Up @@ -6325,6 +6358,11 @@ is-plain-obj@^2.1.0:
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287"
integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==

is-plain-obj@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-3.0.0.tgz#af6f2ea14ac5a646183a5bbdb5baabbc156ad9d7"
integrity sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA==

is-promise@^2.0.0:
version "2.2.2"
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.2.2.tgz#39ab959ccbf9a774cf079f7b40c7a26f763135f1"
Expand Down Expand Up @@ -8350,6 +8388,11 @@ require-main-filename@^2.0.0:
resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-2.0.0.tgz#d0b329ecc7cc0f61649f62215be69af54aa8989b"
integrity sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==

requires-port@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
integrity sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==

resolve-from@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-5.0.0.tgz#c35225843df8f776df21c57557bc087e9dfdfc69"
Expand Down
87 changes: 54 additions & 33 deletions sdk/demos/js/examples/queryVectorStore.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import IndexClient from "@indexnetwork/sdk";
import { OpenAIEmbeddings } from "@langchain/openai";
import { IndexVectorStore } from "@indexnetwork/sdk";
import { Wallet } from "ethers";
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { ConversationalRetrievalQAChain } from "langchain/chains";
import { exit } from "process";

const indexId =
"kjzl6kcym7w8y9sr5qn9o2zg771h0rgv0phmy9hg5lti7w8neoj4ujxdok7skhk";

async function main() {
try {

const wallet = new Wallet(process.env.PRIVATE_KEY);
const indexClient = new IndexClient({
domain: "index.network",
Expand All @@ -17,39 +21,56 @@ async function main() {
await indexClient.authenticate();

const vectorStore = await indexClient.getVectorStore({
embeddings: new OpenAIEmbeddings(),
args: {
temperature: 0.0,
},
});
embeddings: new OpenAIEmbeddings({
openai_api_key: process.env.OPENAI_API_KEY,
openai_model: "text-embedding-ada-002",
}),
sources: [indexId]
})

/* Run vector store search */
const question = "What is $STYLE Protocol?";
const res = await vectorStore.similaritySearch(question, 1);
console.log('Retieved Documents', JSON.stringify(res, null, 3))

/* Create a QA chain */
const model = new ChatOpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-3.5-turbo"
})

const chain = ConversationalRetrievalQAChain.fromLLM(
model,
vectorStore.asRetriever()
);

/* Ask it a question */
const qa_res = await chain.invoke({ question, chat_history: [] });
console.log('Chat response:', JSON.stringify(qa_res, null, 3));

/* Read-Only Functionality */
// These functions are read-only and will not work in the current version of the SDK
try {

// Add a document to the vector store
await vectorStore.addDocuments({
id: "doc1",
text: "This is a test document",
metadata: {
field_1: "value_1",
}
})
} catch (err) {
console.error('Add functionality is omitted with error:', err);
}

try {
// Delete a document from the vector store
await vectorStore.delete(["doc1"])
} catch (err) {
console.error('Delete functionality is omitted with error:', err);
}

const query = "What is $STYLE Protocol?";
const filters = {
indexId: {
$in: [indexId],
},
};

const results = await vectorStore.similaritySearch(query, 5, filters);

console.log(results);
/*
[
WebPage {
url: 'https://styleprotocol.org/',
title: 'Style Protocol',
description: 'Style Protocol is a decentralized protocol for creating, trading, and managing synthetic assets.',
image: 'https://styleprotocol.org/images/hero.png'
},
WebPage {
url: 'https://styleprotocol.org/docs',
title: 'Style Protocol Documentation',
description: 'Style Protocol is a decentralized protocol for creating, trading, and managing synthetic assets.',
image: 'https://styleprotocol.org/images/hero.png'
},
....
]
*/
} catch (err) {
console.error(err);
}
Expand Down
6 changes: 4 additions & 2 deletions sdk/demos/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"type": "module",
"license": "MIT",
"dependencies": {
"@indexnetwork/sdk": "^0.0.10",
"@langchain/openai": "^0.0.28",
"ethers": "^6.12.0",
"uuid": "^9.0.1"
"uuid": "^9.0.1",
"chromadb": "^1.8.1",
"langchain": "^0.1.36",
"openai": "^4.38.5"
}
}
Loading

0 comments on commit 873e675

Please sign in to comment.