diff --git a/Makefile b/Makefile index c24f1b61..60376884 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ ENV=dev help: echo "make build | build the SAM project" + echo "make serve | run the SAM server locally" echo "make clean | remove all installed dependencies and build artifacts" echo "make deps | install all dependencies" echo "make link | create hard links to allow for hot reloading of a built project" diff --git a/python/src/handlers/chat.py b/python/src/handlers/chat.py index 0268bcde..9d7a0370 100644 --- a/python/src/handlers/chat.py +++ b/python/src/handlers/chat.py @@ -1,17 +1,19 @@ -from langchain.vectorstores import Weaviate +import base64 import json import setup def handler(event, context): - params = event.get("queryStringParameters", {}) - query = params.get("query") + question = event.get("body", "") + if event.get("isBase64Encoded", False): + question = base64.b64decode(question) - CLIENT = setup.connect() - index_name = "Work" - text_key = "title" + params = event.get("queryStringParameters", {}) + index_name = params.get("index", "Work") + text_key = params.get("text_key", "title") + attributes = params.get("attributes", "identifier,title").split(",") - weaviate = Weaviate(CLIENT, index_name, text_key) - result = weaviate.similarity_search_by_text(query=query, additional="certainty") + weaviate = setup.weaviate_vector_store(index_name=index_name, text_key=text_key, attributes=attributes) + result = weaviate.similarity_search_by_text(query=question, additional="certainty") return { "statusCode": 200, diff --git a/python/src/setup.py b/python/src/setup.py index 08ede545..39bfc862 100644 --- a/python/src/setup.py +++ b/python/src/setup.py @@ -1,17 +1,29 @@ +from langchain.chat_models import AzureChatOpenAI +from langchain.vectorstores import Weaviate +from typing import List +import openai import os import weaviate -def connect(): +def openai_chat_client(): + deployment = os.getenv("AZURE_OPENAI_LLM_DEPLOYMENT_ID") + resource = os.getenv("AZURE_OPENAI_RESOURCE_NAME") + api_key = os.getenv("AZURE_OPENAI_API_KEY") + + return AzureChatOpenAI(deployment_name=deployment) + +def weaviate_vector_store(index_name: str, text_key: str, attributes: List[str] = []): weaviate_url = os.environ['WEAVIATE_URL'] weaviate_api_key = os.environ['WEAVIATE_API_KEY'] openai_api_key = os.environ['AZURE_OPENAI_API_KEY'] auth_config = weaviate.AuthApiKey(api_key=weaviate_api_key) - return weaviate.Client( + client = weaviate.Client( url=weaviate_url, auth_client_secret=auth_config, additional_headers={ "X-OpenAI-Api-Key": openai_api_key } - ) \ No newline at end of file + ) + return Weaviate(client=client, index_name=index_name, text_key=text_key, attributes=attributes) diff --git a/template.yaml b/template.yaml index 8f4eacbc..1fab7388 100644 --- a/template.yaml +++ b/template.yaml @@ -564,12 +564,12 @@ Resources: WEAVIATE_API_KEY: !Ref WeaviateApiKey WEAVIATE_URL: !Ref WeaviateUrl Events: - GetApiGet: + PostApi: Type: HttpApi Properties: ApiId: !Ref dcApi Path: /chat - Method: GET + Method: POST defaultFunction: Type: AWS::Serverless::Function Properties: