Skip to content
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

Sync numerous updates from older repo #4

Merged
merged 8 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Node Engine Service",
"type": "debugpy",
"request": "launch",
"module": "node_engine.start",
"args": [
"--registry-root",
"${input:registry_root}"
]
}
],
"inputs": [
{
"id": "registry_root",
"description": "Registry root directory",
"default": "examples",
"type": "promptString"
}
]
}
19 changes: 19 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"python.analysis.diagnosticMode": "workspace",
"python.analysis.typeCheckingMode": "basic",
"python.analysis.fixAll": [
"source.unusedImports"
],
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.unusedImports": "explicit",
"source.organizeImports": "explicit",
"source.fixAll": "explicit"
}
},
"python.analysis.exclude": [
"node_engine/libs/tests"
]
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The steps below assume your dev environment has Python 3.10.11+ or 3.11+ install

It's highly recommended to develop with Node Engine using python virtual environments, with all the benefits that come. See https://docs.python.org/3/library/venv.html for details.

## Install Node Engine dependencies
## Install Node Engine and dependencies

These steps will set up all of the prerequisites for running the Node Engine
service locally, along with anything needed for the example scripts and notebooks.
Expand Down Expand Up @@ -114,7 +114,7 @@ the following commands from the root of the project:

# alternative with VS Code debugger:
# Shift+Ctrl+D and choose 'Node Engine Service'
node_engine_service --registry_root examples
node-engine-service --registry-root examples

# Quick Tests

Expand All @@ -131,7 +131,7 @@ the following commands from the root of the project:

- Call the Node Engine service with a sample flow definition:

python3 examples/scripts/invoke-flow.py examples/definitions/simple-continuation-chat.json --session_id sid123 --log_level debug --stream_log
python3 examples/scripts/invoke-flow.py examples/definitions/simple-continuation-chat.json --session-id sid123 --log-level debug --stream-log

- You should see a sample JSON output on screen, without errors, and a context containing "Hello World".

Expand Down Expand Up @@ -161,7 +161,7 @@ Example:
- Open a new console, activate the python virtual environment
- Run this command passing a session ID of your choice:

python3 examples/scripts/invoke-flow.py examples/definitions/simple-cognition.json --session_id sid123 --log_level debug --stream_log
python3 examples/scripts/invoke-flow.py examples/definitions/simple-cognition.json --session-id sid123 --log-level debug --stream-log

## Simple Chat client

Expand All @@ -177,7 +177,7 @@ Example:
- Open a new console, activate the python virtual environment
- Run this command passing the correct session ID:

python3 examples/scripts/debug-service.py --log_level debug sid123
python3 examples/scripts/debug-service.py --log-level debug sid123

- Interact with the chat app and observe logs coming through in the console.

Expand Down
8 changes: 4 additions & 4 deletions docs/DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Example:
touch myapp1/registry.json

# Start service using "myapp1" component registry
node_engine_service --registry_root myapp1
node-engine-service --registry-root myapp1

When looking for `registry.json`, Node Engine service starts by looking in `registry_root` directory, searching up the directory tree until it finds a `registry.json` file. Each `registry.json` found is merged with the previous one, with the first one found taking precedence. This allows you to have a base `registry.json` file and then override it with a custom `registry.json` file in an app directory. The default `registry.json` file in the node_engine directory is merged last.

Expand Down Expand Up @@ -62,9 +62,9 @@ The notebook workflow above is not required (though may be helpful and good prac

This is good for debugging test scripts (non-service code)

- Insert `-m debugpy --listen 5678 --wait-for-client` between `python3` and the script to call. For example if your script is `examples/scripts/test-simple-chat.py "my-session-1" "ok, thanks!"`
- Insert `-m debugpy --listen 5678 --wait-for-client` between `python3` and the script to call. For example if your script is `examples/scripts/test-chat-client.py "my-name" "<session-id>"`

python3 -m debugpy --listen 5678 --wait-for-client examples/scripts/test-simple-chat.py "my-session-1" "ok, thanks!"
python3 -m debugpy --listen 5678 --wait-for-client examples/scripts/test-chat-client.py "my-name" "<session-id>"

- Attach to the process via VS Code (Shift+Ctrl+D and choose 'Attach').

Expand All @@ -73,7 +73,7 @@ This is good for debugging test scripts (non-service code)
We created an agent to help you debug your flows and components. You can run it
in a terminal with:

python3 examples/scripts/debug-service.py <session_id>
python3 examples/scripts/debug-service.py <session-id>

The debug agent will connect to the Node Engine Service and listen to any logs
emitted via SSE for the given session. It will attempt to debug any error
Expand Down
4 changes: 2 additions & 2 deletions docs/node_engine/libs/endpoint_runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ The `endpoint_runner.py` in the libs directory offers the `EndpointRunner` class

- **Execution**: Through the `execute` asynchronous method, the class manages the remote component execution workflow, handling the request and response processing.

- **Getting Component Source**: Additionally, the `get_component_source` method retrieves the source code for the remotely hosted component, aiding in debugging and verification of remote execution.
- **Getting Component Source**: Additionally, the `_source_code` method retrieves the source code for the remotely hosted component, aiding in debugging and verification of remote execution.

- **Error Handling**: Implements robust error handling for cases where the remote invocation fails or returns an unexpected response.

This class is particularly useful for integrating remote components into flow definitions when using the Node Engine, thereby expanding its capabilities and interoperability with different parts of a distributed system.
This class is particularly useful for integrating remote components into flow definitions when using the Node Engine, thereby expanding its capabilities and interoperability with different parts of a distributed system.
66 changes: 35 additions & 31 deletions docs/node_engine/service.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@

# Node Engine - Service

## Endpoints:

1. **/invoke**:
- Method: POST
- Function: Invokes a flow based on a provided flow definition.
- Inputs: `FlowDefinition` object and `Request`.
- Outputs: Updated `FlowDefinition` with the results of the invoked flow.

2. **/invoke_component**:
- Method: POST
- Function: Invokes a single component within a flow.
- Inputs: `component_key` (string), `FlowDefinition` object, and `Request`.
- Outputs: A `FlowStep` object with the result of the invoked component.

3. **/registry**:
- Method: GET
- Function: Lists all components in the registry.
- Outputs: List of dictionaries with key, label, description, and type of each component.

4. **/sse**:
- Method: GET
- Function: Subscribes to server-sent events based on `session_id` and optionally `connection_id`.
- Inputs: `Request`, `session_id` (string), `connection_id` (string, optional).
- Outputs: `EventSourceResponse` with SSE messages.

5. **/emit_sse_message**:
- Method: POST
- Function: Emits an SSE message for subscribed clients.
- Inputs: `SSEMessage` object.
- Outputs: Status of the message emission.
1. **/invoke**:

- Method: POST
- Function: Invokes a flow based on a provided flow definition.
- Inputs: `FlowDefinition` object and `Request`.
- Outputs: Updated `FlowDefinition` with the results of the invoked flow.

2. **/invoke_component**:

- Method: POST
- Function: Invokes a single component within a flow.
- Inputs: `component_key` (string), `FlowDefinition` object, and `Request`.
- Outputs: A `FlowStep` object with the result of the invoked component.

3. **/registry**:

- Method: GET
- Function: Lists all components in the registry.
- Outputs: List of dictionaries with key, label, description, and type of each component.

4. **/sse**:

- Method: GET
- Function: Subscribes to server-sent events based on `session_id` and optionally `connection_id`.
- Inputs: `Request`, `session_id` (string), `connection_id` (string, optional).
- Outputs: `EventSourceResponse` with SSE messages.

5. **/emit_sse_message**:
- Method: POST
- Function: Emits an SSE message for subscribed clients.
- Inputs: `SSEMessage` object.
- Outputs: Status of the message emission.

## Usage Examples:

Expand All @@ -54,9 +57,10 @@ The 'service.py' file integrates the Node Engine within a FastAPI application, e

- **Event Stream**: The event_stream generator handles SSE message delivery and client disconnections within the `/sse` subscription.

- **Runtime**: The `Runtime` class manages flow invocations, component executions, and SSE message emissions.
- **Runtime**: The `Runtime` class manages flow invocations, component executions, and event emissions.

- **Error Handling**: Exception handling is included to manage errors during flow or component invocation, ensuring the flow can exit cleanly with error details.

# Node Engine: service.py
This file is central to the operational capabilities of the Node Engine, and it should be structured and maintained to ensure robust, scalable, and flexible service interactions.

This file is central to the operational capabilities of the Node Engine, and it should be structured and maintained to ensure robust, scalable, and flexible service interactions.
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Run this script in a terminal to have a debugger agent help you with your develo

To run:

- Activate the virtual environment (`.venv`)
- Execute the script

```
python examples/scripts/debug-service.py <session_id>
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"key": "agent_response",
"session_id": "test",
"context": {
"stream_log": true,
"agent": {
"id": "default",
"name": "IC Chatbot",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"key": "get_messages",
"context": {
"stream_log": true
},
"flow": [
{
"key": "get_messages",
Expand Down
3 changes: 3 additions & 0 deletions examples/apps/simple-chat-client/definitions/user-input.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"key": "user_input",
"context": {
"stream_log": true
},
"flow": [
{
"key": "store_input",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from print_color import print

from node_engine.client import invoke
from node_engine.client import NodeEngineClient
from node_engine.models.flow_definition import FlowDefinition

# set up file locations
Expand All @@ -32,7 +32,7 @@ async def generate_response(session_id: str) -> None:
update={"session_id": session_id}
)

result = await invoke(flow_definition)
result = await NodeEngineClient().invoke(flow_definition)

if result.status.error:
print(result.status.error, tag="error", tag_color="red")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib
from typing import Any

from node_engine.client import invoke
from node_engine.client import NodeEngineClient
from node_engine.models.flow_definition import FlowDefinition

# set up file locations
Expand All @@ -29,7 +29,7 @@ async def get_messages(session_id: str) -> dict[str, dict[str, str]]:
update={"session_id": session_id}
)

result = await invoke(flow_definition=flow_definition)
result = await NodeEngineClient().invoke(flow_definition=flow_definition)

if result.status.error:
# throw exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import pathlib
import time
from typing import Any
from node_engine.client import NodeEngineClient

from node_engine.client import invoke
from node_engine.models.flow_definition import FlowDefinition

# set up file locations
Expand Down Expand Up @@ -42,7 +42,7 @@ async def send_message(message, session_id, user_name) -> None:
}
)

result = await invoke(flow_definition)
result = await NodeEngineClient().invoke(flow_definition)

if result.status.error:
# throw exception
Expand Down
2 changes: 1 addition & 1 deletion examples/apps/simple-chat-client/scripts/pages/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from nicegui import Client, app, ui
from print_color import print

from examples.libs.sse_listener import SSEListener
from node_engine.sse_listener import SSEListener

from .. import theme
from ..functions.generate_response import generate_response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@


async def scroll_to_bottom(threshold: int = 200) -> None:
await ui.run_javascript(
ui.run_javascript(
f"""
if (window.scrollY + window.innerHeight >= document.body.scrollHeight - {threshold}) {{
window.scrollTo(0, document.body.scrollHeight);
}}
""",
respond=False,
)
8 changes: 1 addition & 7 deletions examples/apps/simple-chat-client/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ set -e
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && cd ../../.. && pwd)"
cd "$ROOT"

# Activate virtual environment
if [ ! -d "${ROOT}/.venv" ]; then
echo "${ROOT}/.venv directory not found"
exit 1
else
source ${ROOT}/.venv/bin/activate
fi
./tools/activate-venv.sh

# Start the app under examples/apps/simple-chat-client
uvicorn examples.apps.simple-chat-client.app:app --reload
2 changes: 1 addition & 1 deletion examples/components/background_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import asyncio

from node_engine.models.flow_definition import FlowDefinition
from node_engine.models.node_engine_component import NodeEngineComponent
from node_engine.libs.node_engine_component import NodeEngineComponent
from node_engine.models.flow_step import FlowStep


Expand Down
2 changes: 1 addition & 1 deletion examples/components/debug_agent_response.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft. All rights reserved.

from node_engine.libs.azure_openai_chat_completion import AzureOpenAIChatCompletion
from node_engine.models.node_engine_component import NodeEngineComponent
from node_engine.libs.node_engine_component import NodeEngineComponent
from node_engine.models.flow_step import FlowStep


Expand Down
2 changes: 1 addition & 1 deletion examples/components/emit_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json

from node_engine.models.node_engine_component import NodeEngineComponent
from node_engine.libs.node_engine_component import NodeEngineComponent
from node_engine.models.flow_step import FlowStep


Expand Down
2 changes: 1 addition & 1 deletion examples/components/evaluate_continue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import asyncio

from node_engine.models.node_engine_component import NodeEngineComponent
from node_engine.libs.node_engine_component import NodeEngineComponent
from node_engine.models.flow_step import FlowStep


Expand Down
2 changes: 1 addition & 1 deletion examples/components/extract_embeddings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft. All rights reserved.

from node_engine.libs.azure_openai_embeddings import AzureOpenAIEmbeddings
from node_engine.models.node_engine_component import NodeEngineComponent
from node_engine.libs.node_engine_component import NodeEngineComponent
from node_engine.models.flow_step import FlowStep


Expand Down
2 changes: 1 addition & 1 deletion examples/components/extract_memories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from node_engine.libs.azure_openai_chat_completion import AzureOpenAIChatCompletion
from node_engine.libs.storage import Storage
from node_engine.libs.telemetry import Timer
from node_engine.models.node_engine_component import NodeEngineComponent
from node_engine.libs.node_engine_component import NodeEngineComponent
from node_engine.models.flow_step import FlowStep


Expand Down
Loading
Loading