Skip to content

Commit

Permalink
Port JS chat code over from chat-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Aug 28, 2023
1 parent 18ba72a commit 5a70b84
Show file tree
Hide file tree
Showing 25 changed files with 2,289 additions and 833 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ $RECYCLE.BIN/

.vscode
/samconfig.toml
/samconfig.yaml
/env.json
/env.*.json
/*.parameters
Expand Down
1 change: 0 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
cd node && npm run lint && npm run prettier && cd -
cd python && ruff check . && cd -
3 changes: 1 addition & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
nodejs 16.14.0
nodejs 18.16.1
java corretto-19.0.1.10.1
python 3.9.17
35 changes: 9 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,28 @@ help:
echo "make test | run all tests"
echo "make cover | run all tests with coverage"
echo "make env ENV=[env] | activate env.\$$ENV.json file (default: dev)"
echo "make deps-node | install node dependencies"
echo "make deps-python | install python dependencies"
echo "make style-node | run node code style check"
echo "make style-python | run python code style check"
echo "make test-node | run node tests"
echo "make test-python | run python tests"
echo "make cover-node | run node tests with coverage"
echo "make cover-python | run python tests with coverage"
echo "make deps | install dependencies"
echo "make style | run code style check"
echo "make test | run tests"
echo "make cover | run tests with coverage"
.aws-sam/build.toml: ./template.yaml node/package-lock.json node/src/package-lock.json python/requirements.txt python/src/requirements.txt
sam build --cached --parallel
deps-node:
deps:
cd node && npm ci
cover-node:
cover:
cd node && npm run test:coverage
style-node:
style:
cd node && npm run prettier
test-node:
test:
cd node && npm run test
deps-python:
cd python && pip install -r requirements.txt
cover-python:
cd python && coverage run --include='src/**/*' -m unittest -v && coverage report
style-python:
cd python && ruff check .
test-python:
cd python && python -m unittest -v
build: .aws-sam/build.toml
link: build
cd python/src && for src in *.py **/*.py; do for target in $$(find ../../.aws-sam/build -maxdepth 1 -type d); do if [[ -f $$target/$$src ]]; then ln -f $$src $$target/$$src; fi; done; done
cd node/src && for src in *.js *.json **/*.js **/*.json; do for target in $$(find ../../.aws-sam/build -maxdepth 1 -type d); do if [[ -f $$target/$$src ]]; then ln -f $$src $$target/$$src; fi; done; done
serve: link
sam local start-api --host 0.0.0.0 --log-file dc-api.log
deps: deps-node deps-python
style: style-node style-python
test: test-node test-python
cover: cover-node cover-python
env:
ln -fs ./env.${ENV}.json ./env.json
secrets:
ln -s ../tfvars/dc-api/* .
clean:
rm -rf .aws-sam node/node_modules node/src/node_modules python/**/__pycache__ python/.coverage python/.ruff_cache
rm -rf .aws-sam node/node_modules node/src/node_modules
79 changes: 79 additions & 0 deletions node/src/embedded-weaviate-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const { WeaviateStore } = require("langchain/vectorstores/weaviate");
const { Document } = require("langchain/document");

class EmbeddedWeaviateStore extends WeaviateStore {
constructor(...args) {
super(...args);
}

async similaritySearch(query, k = 4, filter = undefined) {
try {
let builder = await this.client.graphql
.get()
.withClassName(this.indexName)
.withFields(this.queryAttrs.join(" "))
.withNearText({
concepts: [query],
})
.withLimit(k);

if (filter?.where) {
builder = builder.withWhere(filter.where);
}

const result = await builder.do();

const documents = [];
for (const data of result.data.Get[this.indexName]) {
const { [this.textKey]: text, ...rest } = data;

documents.push(
new Document({
pageContent: text,
metadata: rest,
})
);
}
return documents;
} catch (e) {
throw Error(`'Error in similaritySearch' ${e}`);
}
}

async similaritySearchWithScore(query, k = 4, filter = undefined) {
try {
let builder = await this.client.graphql
.get()
.withClassName(this.indexName)
.withFields(`${this.queryAttrs.join(" ")} _additional { distance }`)
.withNearText({
concepts: [query],
})
.withLimit(k);

if (filter?.where) {
builder = builder.withWhere(filter.where);
}

const result = await builder.do();

const documents = [];
for (const data of result.data.Get[this.indexName]) {
const { [this.textKey]: text, _additional, ...rest } = data;

documents.push([
new Document({
pageContent: text,
metadata: rest,
}),
_additional.distance,
]);
}
return documents;
} catch (e) {
throw Error(`'Error in similaritySearch' ${e}`);
}
}
}

module.exports = { EmbeddedWeaviateStore };
23 changes: 23 additions & 0 deletions node/src/handlers/chat-endpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { wrap } = require("./middleware");
const Honeybadger = require("../honeybadger-setup");

/**
* ChatEndpoint - Returns the function URL of the streaming chat handler
*/
exports.handler = wrap(async (event) => {
try {
return {
statusCode: 200,
body: JSON.stringify({
url: process.env.CHAT_ENDPOINT,
auth: event.userToken.sign(),
}),
};
} catch (error) {
await Honeybadger.notifyAsync(error);
return {
statusCode: 401,
body: "Error: " + error.message,
};
}
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# ruff: noqa: E501
def prompt_template():
return """Using all of the provided source documents, create a helpful and thorough answer to the supplied question.
function promptTemplate() {
return `Using all of the provided source documents, create a helpful and thorough answer to the supplied question.
If you don't know the answer, just say that you don't know. Don't try to make up an answer, but you should use the documents provided in order to ground your response.
It may be helpful to explain why a provided document does not pertain to the query as well.
Feel free to reference various aspects of the sources in your explanation, but please don't include the full sources in the answer.
Expand Down Expand Up @@ -188,10 +187,19 @@ def prompt_template():
QUESTION: {question}
=========
HELPFUL ANSWER:"""
HELPFUL ANSWER: `;
}

def document_template(attributes):
lines = (["Content: {page_content}", "Metadata:"] +
[f" {attribute}: {{{attribute}}}" for attribute in attributes] +
["Source: {source}"])
return "\n".join(lines)
function documentTemplate(attributes) {
let lines = [
"Content: {pageContent}",
"Metadata:",
[...attributes.map((attribute) => ` ${attribute}: {${attribute}}`)].join(
"\n"
),
"Source: {source}",
];
return lines.join("\n");
}

module.exports = { promptTemplate, documentTemplate };
Loading

0 comments on commit 5a70b84

Please sign in to comment.