Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Shimmy#9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Shimmy #9
Changes from all commits
8de7d90d81c039c760c9ae3c7ce593c0819ab9c3b0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -119,7 +119,6 @@ The agent uses **two separate LLM instances** — `self.llm` for chat responses | ||
| ├── manual_agent_run.py # allows testing of any LLM agent on a couple of example inputs | ||
| ├── utils.py # shared test helpers | ||
| ├── test_example_inputs.py # pytests for the example input files | ||
| ├── test_index.py # pytests | ||
| └── test_module.py # pytests | ||
| ``` | ||
| @@ -130,18 +129,18 @@ To test your function, you can run the unit tests, call the code directly throug | ||
| ### Run Unit Tests | ||
| You can run the unit tests using `pytest`. | ||
| You can run the unit tests using `pytest`. Run it from the repository root with `PYTHONPATH=.` set (as CI does) so the `tests` and `src` packages resolve correctly: | ||
| ```bash | ||
| pytest | ||
| PYTHONPATH=. pytest | ||
| ``` | ||
| ### Run the Chat Script | ||
| You can run the Python function itself. Make sure to have a main function in either `src/module.py` or `index.py`. | ||
| You can run the Python function itself directly — `index.py` wires `chat_module`/`chat_health_module` into `lf_toolkit`'s RPC server, the same way shimmy invokes it inside the container. This requires the `EVAL_IO`/`EVAL_RPC_TRANSPORT` environment variables shimmy would normally set (see `lf_toolkit`'s docs), so prefer the Docker or `manual_agent_run.py` routes below for everyday testing. | ||
| ```bash | ||
| python src/module.py | ||
| python index.py | ||
| ``` | ||
| You can also use the `manual_agent_run.py` script to test the agents with example inputs from Lambda Feedback questions and synthetic conversations. | ||
| @@ -173,33 +172,41 @@ docker run -e OPENAI_API_KEY={your key} -e OPENAI_MODEL={your LLM model name} -p | ||
| docker run --env-file .env -it --name my-lambda-container -p 8080:8080 llm_chat | ||
| ``` | ||
| This will start the chat function and expose it on port `8080` and it will be open to be curl: | ||
| This starts shimmy (the [Lambda Feedback shim](https://github.com/lambda-feedback/shimmy)) as the container's entrypoint, which spawns this function as a worker subprocess and exposes it on port `8080` as the muEd chat API: | ||
m-messer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ```bash | ||
| curl --location 'http://localhost:8080/2015-03-31/functions/function/invocations' \ | ||
| curl --location 'http://localhost:8080/chat' \ | ||
| --header 'Content-Type: application/json' \ | ||
| --data '{"body":"{\"messages\": [{\"role\": \"USER\", \"content\": \"hi\"}]}"}' | ||
| --header 'X-Api-Version: 0.1.0' \ | ||
| --data '{"messages": [{"role": "USER", "content": "hi"}]}' | ||
| ``` | ||
| Health check: | ||
| ```bash | ||
| curl --location 'http://localhost:8080/chat/health' \ | ||
| --header 'X-Api-Version: 0.1.0' | ||
| ``` | ||
| #### Call Docker Container | ||
| ##### A. Call Docker with Python Requests | ||
| In the `tests/` folder you can find the `manual_agent_requests.py` script that calls the POST URL of the running docker container. It reads any kind of input files with the expected schema. You can use this to test your curl calls of the chatbot. | ||
| In the `tests/` folder you can find the `manual_agent_requests.py` script that calls the `/chat` and `/chat/health` routes of the running docker container. It reads any kind of input files with the expected schema. You can use this to test your curl calls of the chatbot. | ||
| ##### B. Call Docker Container through API request | ||
| POST URL: | ||
| ```bash | ||
| http://localhost:8080/2015-03-31/functions/function/invocations | ||
| http://localhost:8080/chat | ||
| ``` | ||
| Per the [muEd `ChatRequest` schema](https://mued.org/), only `messages` is required; `conversationId`, `user`, `context`, and `configuration` are all optional. | ||
| Per the [muEd `ChatRequest` schema](https://mued.org/), only `messages` is required; `conversationId`, `user`, `context`, and `configuration` are all optional. Requests may include an `X-Api-Version: 0.1.0` header. | ||
| **Minimal request — only required components** (stringified within `body` for the AWS Lambda Runtime Interface Emulator): | ||
| **Minimal request — only required components:** | ||
| ```JSON | ||
| {"body":"{\"messages\": [{\"role\": \"USER\", \"content\": \"hi\"}]}"} | ||
| {"messages": [{"role": "USER", "content": "hi"}]} | ||
| ``` | ||
| **Full request as Lambda Feedback sends it** — all optional fields populated: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,14 @@ | ||
| import json | ||
| from pydantic import ValidationError | ||
| from lf_toolkit import create_server, run | ||
| from lf_toolkit.chat import ChatRequest | ||
| from src.module import chat_module | ||
| from src.module import chat_health_module, chat_module | ||
| def handler(event, context): | ||
| """ | ||
| Lambda handler function | ||
| """ | ||
| print("Received event:", json.dumps(event)) | ||
| def main(): | ||
| server = create_server() | ||
| server.chat(chat_module) | ||
| server.chat_health(chat_health_module) | ||
| run(server) | ||
| if "body" in event: | ||
| try: | ||
| event = json.loads(event["body"]) | ||
| except json.JSONDecodeError: | ||
| return { | ||
| "statusCode": 400, | ||
| "body": "Invalid JSON format in the body. Please check the input.", | ||
| } | ||
| try: | ||
| request = ChatRequest.model_validate(event) | ||
| except ValidationError as e: | ||
| return {"statusCode": 400, "body": e.json()} | ||
| try: | ||
| result = chat_module(request) | ||
| except Exception as e: | ||
| return { | ||
| "statusCode": 500, | ||
| "body": f"An error occurred within the chat_module(): {str(e)}", | ||
| } | ||
| response = {"statusCode": 200, "body": result.model_dump_json()} | ||
| return response | ||
| if __name__ == "__main__": | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| import time | ||
| from langchain_core.messages import HumanMessage, AIMessage, SystemMessage | ||
| from lf_toolkit.chat import ChatRequest, ChatResponse, Message | ||
| from lf_toolkit.shared.mued_api_v0_1_0 import Role | ||
| from lf_toolkit.chat import ChatCapabilities, ChatHealthResponse, ChatRequest, ChatResponse, Message | ||
| from lf_toolkit.shared.mued_api_v0_1_0 import DataPolicySupport, HealthStatus, Role | ||
| from src.agent.context import parse_json_to_prompt | ||
| from src.agent.agent import invoke_base_agent | ||
| @@ -61,6 +61,22 @@ def chat_module(request: ChatRequest) -> ChatResponse: | ||
| ) | ||
| def chat_health_module() -> ChatHealthResponse: | ||
| """ | ||
| Health-check entry point — reports whether this chat function is up and | ||
| what it supports, for the shim's GET /chat/health. | ||
| """ | ||
| return ChatHealthResponse( | ||
| status=HealthStatus.OK, | ||
| capabilities=ChatCapabilities( | ||
| supportsChat=True, | ||
| supportsUserPreferences=True, | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to update this to False | ||
| supportsStreaming=False, | ||
| supportsDataPolicy=DataPolicySupport.NOT_SUPPORTED, | ||
| ), | ||
| ) | ||
| def _to_langchain_messages(messages): | ||
| result = [] | ||
| for m in messages: | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here as maybe we should now rename the deployment path to microservices-base