-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #276 from nulib/5279-chat-sync
Get sync chat function working
- Loading branch information
Showing
7 changed files
with
99 additions
and
328 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,43 @@ | ||
import secrets # noqa | ||
import json | ||
import logging | ||
import os | ||
from http_event_config import HTTPEventConfig | ||
from helpers.http_response import HTTPResponse | ||
from agent.metrics_handler import MetricsHandler | ||
from agent.search_agent import SearchAgent | ||
from handlers.model import chat_model | ||
from event_config import EventConfig | ||
from honeybadger import honeybadger | ||
|
||
honeybadger.configure() | ||
logging.getLogger('honeybadger').addHandler(logging.StreamHandler()) | ||
|
||
RESPONSE_TYPES = { | ||
"base": ["answer", "ref", "context"], | ||
"debug": ["answer", "attributes", "azure_endpoint", "deployment_name", "is_superuser", "k", "openai_api_version", "prompt", "question", "ref", "temperature", "text_key", "token_counts", "context"], | ||
"log": ["answer", "deployment_name", "is_superuser", "k", "openai_api_version", "prompt", "question", "ref", "size", "source_documents", "temperature", "token_counts"], | ||
"error": ["question", "error", "source_documents"] | ||
} | ||
|
||
def handler(event, context): | ||
config = HTTPEventConfig(event) | ||
config = EventConfig(event) | ||
|
||
if not config.is_logged_in: | ||
return {"statusCode": 401, "body": "Unauthorized"} | ||
|
||
if config.question is None or config.question == "": | ||
return {"statusCode": 400, "body": "Question cannot be blank"} | ||
|
||
if not os.getenv("SKIP_LLM_REQUEST"): | ||
config.setup_llm_request() | ||
response = HTTPResponse(config) | ||
final_response = response.prepare_response() | ||
if "error" in final_response: | ||
logging.error(f'Error: {final_response["error"]}') | ||
return {"statusCode": 500, "body": "Internal Server Error"} | ||
else: | ||
return {"statusCode": 200, "body": json.dumps(reshape_response(final_response, 'debug' if config.debug_mode else 'base'))} | ||
|
||
return {"statusCode": 200} | ||
model = chat_model(model=config.model, streaming=False) | ||
search_agent = SearchAgent(model=model) | ||
result = MetricsHandler() | ||
search_agent.invoke(config.question, config.ref, forget=config.forget, callbacks=[result]) | ||
|
||
def reshape_response(response, type): | ||
return {k: response[k] for k in RESPONSE_TYPES[type]} | ||
return { | ||
"statusCode": 200, | ||
"headers": { | ||
"Content-Type": "application/json" | ||
}, | ||
"body": json.dumps({ | ||
"answer": result.answers, | ||
"is_dev_team": config.api_token.is_dev_team(), | ||
"is_superuser": config.api_token.is_superuser(), | ||
"k": config.k, | ||
"model": config.model, | ||
"question": config.question, | ||
"ref": config.ref, | ||
"artifacts": result.artifacts, | ||
"token_counts": result.accumulator, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Dev/Test Dependencies | ||
ruff~=0.1.0 | ||
ruff~=0.2.0 | ||
coverage~=7.3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters