Skip to content

Commit

Permalink
Parameterize setup and chat; continue to build out prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Jun 22, 2023
1 parent a644b76 commit 1412795
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 10 additions & 8 deletions python/src/handlers/chat.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
18 changes: 15 additions & 3 deletions python/src/setup.py
Original file line number Diff line number Diff line change
@@ -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
}
)
)
return Weaviate(client=client, index_name=index_name, text_key=text_key, attributes=attributes)
4 changes: 2 additions & 2 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1412795

Please sign in to comment.