├── .DS_Store ├── .gitignore ├── README.md ├── insert.js ├── package.json ├── search.js └── yarn.lock /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/numbbbbb/api2d-vector-demo/e58d5adf22ef5f8643b0f50a0e3946cd5b062b77/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Optional stylelint cache 58 | .stylelintcache 59 | 60 | # Microbundle cache 61 | .rpt2_cache/ 62 | .rts2_cache_cjs/ 63 | .rts2_cache_es/ 64 | .rts2_cache_umd/ 65 | 66 | # Optional REPL history 67 | .node_repl_history 68 | 69 | # Output of 'npm pack' 70 | *.tgz 71 | 72 | # Yarn Integrity file 73 | .yarn-integrity 74 | 75 | # dotenv environment variable files 76 | .env 77 | .env.development.local 78 | .env.test.local 79 | .env.production.local 80 | .env.local 81 | 82 | # parcel-bundler cache (https://parceljs.org/) 83 | .cache 84 | .parcel-cache 85 | 86 | # Next.js build output 87 | .next 88 | out 89 | 90 | # Nuxt.js build / generate output 91 | .nuxt 92 | dist 93 | 94 | # Gatsby files 95 | .cache/ 96 | # Comment in the public line in if your project uses Gatsby and not Next.js 97 | # https://nextjs.org/blog/next-9-1#public-directory-support 98 | # public 99 | 100 | # vuepress build output 101 | .vuepress/dist 102 | 103 | # vuepress v2.x temp and cache directory 104 | .temp 105 | .cache 106 | 107 | # Docusaurus cache and generated files 108 | .docusaurus 109 | 110 | # Serverless directories 111 | .serverless/ 112 | 113 | # FuseBox cache 114 | .fusebox/ 115 | 116 | # DynamoDB Local files 117 | .dynamodb/ 118 | 119 | # TernJS port file 120 | .tern-port 121 | 122 | # Stores VSCode versions used for testing VSCode extensions 123 | .vscode-test 124 | 125 | # yarn v2 126 | .yarn/cache 127 | .yarn/unplugged 128 | .yarn/build-state.yml 129 | .yarn/install-state.gz 130 | .pnp.* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # api2d-vector-demo 2 | 使用 API2D 向量数据库实现简单的知识库 3 | 4 | ## 安装 5 | 6 | 需要安装好 `node` 和 `yarn`。 7 | 8 | `yarn install` 9 | 10 | ## 执行 11 | 12 | `node insert.js` 13 | 14 | `node search.js` 15 | -------------------------------------------------------------------------------- /insert.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios'); 2 | 3 | const knowledge = ['我喜欢吃水果', '我不喜欢吃蔬菜', '对于肉类,有时候我喜欢吃,有时候不喜欢吃,比如膻味很重的羊肉我就不爱吃']; 4 | 5 | const getEmbeddings = async (str) => await axios.post('https://openai.api2d.net/v1/embeddings', { 6 | model: 'text-embedding-ada-002', 7 | input: str 8 | }, { 9 | headers: { 10 | 'Content-Type': 'application/json', 11 | 'Authorization': 'Bearer fk....' // 替换成你的 Forward key 12 | } 13 | }); 14 | 15 | const insertIntoVectorDB = async (text, uuid, embedding) => await axios.post('https://openai.api2d.net/vector', { 16 | text, 17 | uuid, 18 | embedding 19 | }, { 20 | headers: { 21 | 'Content-Type': 'application/json', 22 | 'Authorization': 'Bearer fk....' // 替换成你的 Forward key 23 | } 24 | }); 25 | 26 | const getUUID = async () => await axios.get('https://openai.api2d.net/vector/uuid', { 27 | headers: { 28 | 'Content-Type': 'application/json' 29 | } 30 | }); 31 | 32 | const insertData = async () => { 33 | const uuid = (await getUUID()).data.uuid; 34 | 35 | console.log('uuid: ', uuid); 36 | 37 | for (let text of knowledge) { 38 | let embeddingResponse = await getEmbeddings(text); 39 | console.log('输入文本:', text, ',embeddings 请求结果:', embeddingResponse.data); 40 | 41 | let insertResponse = await insertIntoVectorDB(text, uuid, embeddingResponse.data.data[0].embedding); 42 | 43 | console.log('写入数据库请求结果:', insertResponse.data); 44 | } 45 | } 46 | 47 | insertData(); 48 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vector-demo", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "license": "MIT", 6 | "dependencies": { 7 | "axios": "^1.3.5" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /search.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios'); 2 | 3 | const question = '你喜不喜欢吃肉'; 4 | 5 | const searchable_id = '186009-1225e1de-5d7b-4750-bfa5-c194463e5be8'; 6 | 7 | const getEmbeddings = async (str) => await axios.post('https://openai.api2d.net/v1/embeddings', { 8 | model: 'text-embedding-ada-002', 9 | input: str 10 | }, { 11 | headers: { 12 | 'Content-Type': 'application/json', 13 | 'Authorization': 'Bearer fk....' // 替换成你的 Forward key 14 | } 15 | }); 16 | 17 | const queryVectorDB = async (embedding) => await axios.post('https://openai.api2d.net/vector/search', { 18 | embedding, 19 | searchable_id 20 | }, { 21 | headers: { 22 | 'Content-Type': 'application/json', 23 | 'Authorization': 'Bearer fk....' // 替换成你的 Forward key 24 | } 25 | }); 26 | 27 | const editByGPT = async (text) => await axios.post('https://openai.api2d.net/v1/chat/completions', { 28 | model: 'gpt-3.5-turbo-0301', 29 | messages: [ 30 | { 31 | "role": "system", 32 | "content": "你是一个问答机器人,我会给你用户的问题,以及这个问题对应的答案,请你帮忙润色,整理成一个可读性更高的回复。" 33 | }, 34 | { 35 | "role": "user", 36 | "content": `问题是:${question},对应的答案是:${text}` 37 | }, 38 | ], 39 | }, { 40 | headers: { 41 | 'Content-Type': 'application/json', 42 | 'Authorization': 'Bearer fk....' // 替换成你的 Forward key 43 | } 44 | }); 45 | 46 | const askQuestion = async () => { 47 | let embeddingResponse = await getEmbeddings(question); 48 | console.log(embeddingResponse.data.data[0].embedding); 49 | let queryResponse = await queryVectorDB(embeddingResponse.data.data[0].embedding); 50 | 51 | console.log('搜索结果:', queryResponse.data.data.Get.Text); 52 | console.log('答案:', queryResponse.data.data.Get.Text[0].text); 53 | 54 | let gptResponse = await editByGPT(queryResponse.data.data.Get.Text[0].text); 55 | 56 | console.log('润色结果:', gptResponse.data.choices[0].message); 57 | } 58 | 59 | askQuestion(); 60 | 61 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | asynckit@^0.4.0: 6 | version "0.4.0" 7 | resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" 8 | integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== 9 | 10 | axios@^1.3.5: 11 | version "1.3.5" 12 | resolved "https://registry.yarnpkg.com/axios/-/axios-1.3.5.tgz#e07209b39a0d11848e3e341fa087acd71dadc542" 13 | integrity sha512-glL/PvG/E+xCWwV8S6nCHcrfg1exGx7vxyUIivIA1iL7BIh6bePylCfVHwp6k13ao7SATxB6imau2kqY+I67kw== 14 | dependencies: 15 | follow-redirects "^1.15.0" 16 | form-data "^4.0.0" 17 | proxy-from-env "^1.1.0" 18 | 19 | combined-stream@^1.0.8: 20 | version "1.0.8" 21 | resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" 22 | integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== 23 | dependencies: 24 | delayed-stream "~1.0.0" 25 | 26 | delayed-stream@~1.0.0: 27 | version "1.0.0" 28 | resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" 29 | integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== 30 | 31 | follow-redirects@^1.15.0: 32 | version "1.15.2" 33 | resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13" 34 | integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA== 35 | 36 | form-data@^4.0.0: 37 | version "4.0.0" 38 | resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" 39 | integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== 40 | dependencies: 41 | asynckit "^0.4.0" 42 | combined-stream "^1.0.8" 43 | mime-types "^2.1.12" 44 | 45 | mime-db@1.52.0: 46 | version "1.52.0" 47 | resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" 48 | integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== 49 | 50 | mime-types@^2.1.12: 51 | version "2.1.35" 52 | resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" 53 | integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw== 54 | dependencies: 55 | mime-db "1.52.0" 56 | 57 | proxy-from-env@^1.1.0: 58 | version "1.1.0" 59 | resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" 60 | integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== 61 | --------------------------------------------------------------------------------