Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into update-tutorials-to-u…
Browse files Browse the repository at this point in the history
…se-cookiecutter
  • Loading branch information
rjambrecic committed Nov 11, 2024
2 parents e2d93e1 + 9ce1c7a commit 9417aae
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 66 deletions.
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
"filename": "docs/docs/en/user-guide/adapters/fastapi_nats/index.md",
"hashed_secret": "3be2e9dd1980856faddf5bd205e3ff96b24776aa",
"is_verified": false,
"line_number": 301,
"line_number": 315,
"is_secret": false
}
],
Expand All @@ -179,7 +179,7 @@
"filename": "docs/docs/en/user-guide/adapters/nats/index.md",
"hashed_secret": "3be2e9dd1980856faddf5bd205e3ff96b24776aa",
"is_verified": false,
"line_number": 153,
"line_number": 160,
"is_secret": false
}
],
Expand Down
41 changes: 24 additions & 17 deletions docs/docs/en/user-guide/adapters/fastapi/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,38 +214,38 @@ Alternatively, you can use [**Cookiecutter**](../../cookiecutter/index.md), whic

### Step-by-Step Breakdown

#### 1. **Import Required Modules**
#### 1. **Define Workflow**

To get started, define the workflow that your application will use. This is where you specify how the agents interact and what they do. Here's a simple example of a workflow definition:

```python
{! docs_src/getting_started/fastapi/my_fastagency_app/my_fastagency_app/workflow.py [ln:1-47] !}
```

#### 2. **Import Required Modules**

=== "Mesop"

To get started, import the required modules from the **FastAgency** and **AutoGen**. These imports provide the essential building blocks for creating agents, workflows, and integrating [**`MesopUI`**](../../../api/fastagency/ui/mesop/MesopUI.md). Additionally, import the [**`FastAPIAdapter`**](../../../api/fastagency/adapters/fastapi/FastAPIAdapter.md) class to expose the workflows as a [**REST API**](https://en.wikipedia.org/wiki/REST){target="_blank"}.
Next, import the required modules from the **FastAgency**. Import the [**`FastAPIAdapter`**](../../../api/fastagency/adapters/fastapi/FastAPIAdapter.md) class to expose the workflows as a [**REST API**](https://en.wikipedia.org/wiki/REST){target="_blank"}.

```python hl_lines="8"
{!> docs_src/getting_started/fastapi/main_1_fastapi.py [ln:1-9] !}
```python hl_lines="3"
{!> docs_src/getting_started/fastapi/my_fastagency_app/my_fastagency_app/deployment/main_1_fastapi.py [ln:1-4] !}
```

=== "Custom REST API and Websocket"

To get started, import the required modules from the **FastAgency** and **AutoGen**. These imports provide the essential building blocks for creating agents, workflows, and integrating with the custom client. Additionally, import the [**`FastAPIAdapter`**](../../../api/fastagency/adapters/fastapi/FastAPIAdapter.md) and [**`HTMLResponse`**](https://fastapi.tiangolo.com/advanced/custom-response/#html-response){target="_blank"} class to expose the workflows as a [**REST API**](https://en.wikipedia.org/wiki/REST){target="_blank"}.
Next, import the required modules from the **FastAgency** and **AutoGen**. These imports provide the essential building blocks for creating agents, workflows, and integrating with the custom client. Additionally, import the [**`FastAPIAdapter`**](../../../api/fastagency/adapters/fastapi/FastAPIAdapter.md) and [**`HTMLResponse`**](https://fastapi.tiangolo.com/advanced/custom-response/#html-response){target="_blank"} class to expose the workflows as a [**REST API**](https://en.wikipedia.org/wiki/REST){target="_blank"}.

```python hl_lines="6 9"
{!> docs_src/getting_started/fastapi/main_fastapi_custom_client.py [ln:1-10] !}
```

#### 2. **Define Workflow**

Next, define the workflow that your application will use. This is where you specify how the agents interact and what they do. Here's a simple example of a workflow definition:

```python
{! docs_src/getting_started/mesop/my_fastagency_app/my_fastagency_app/local/main_console.py [ln:9-53] !}
```

#### 3. **Define FastAgency Application**

Create an instance of the [**`FastAPIAdapter`**](../../../api/fastagency/adapters/fastapi/FastAPIAdapter.md) and pass your workflow to it. Then, include a router to the [**FastAPI**](https://fastapi.tiangolo.com/){target="_blank"} application. The adapter will have all [**REST API**](https://en.wikipedia.org/wiki/REST){target="_blank"} and [**WebSocket**](https://en.wikipedia.org/wiki/WebSocket){target="_blank"} routes for communicating with the client.

```python hl_lines="1 4"
{!> docs_src/getting_started/fastapi/main_1_fastapi.py [ln:55-58] !}
{!> docs_src/getting_started/fastapi/my_fastagency_app/my_fastagency_app/deployment/main_1_fastapi.py [ln:8-11] !}
```

=== "Mesop"
Expand All @@ -256,7 +256,7 @@ Create an instance of the [**`FastAPIAdapter`**](../../../api/fastagency/adapter

!!! note "main_2_mesop.py"
```python
{!> docs_src/getting_started/fastapi/main_2_mesop.py [ln:1-11] !}
{!> docs_src/getting_started/fastapi/my_fastagency_app/my_fastagency_app/deployment/main_2_mesop.py [ln:1-15] !}
```

=== "Custom REST API and Websocket"
Expand All @@ -275,17 +275,24 @@ Please copy and paste the following code into the same folder, using the file na

=== "Mesop"

<details>
<summary>workflow.py</summary>
```python
{!> docs_src/getting_started/fastapi/my_fastagency_app/my_fastagency_app/workflow.py !}
```
</details>

<details>
<summary>main_1_fastapi.py</summary>
```python
{!> docs_src/getting_started/fastapi/main_1_fastapi.py !}
{!> docs_src/getting_started/fastapi/my_fastagency_app/my_fastagency_app/deployment/main_1_fastapi.py !}
```
</details>

<details>
<summary>main_2_mesop.py</summary>
```python
{!> docs_src/getting_started/fastapi/main_2_mesop.py !}
{!> docs_src/getting_started/fastapi/my_fastagency_app/my_fastagency_app/deployment/main_2_mesop.py !}
```
</details>

Expand Down
70 changes: 42 additions & 28 deletions docs/docs/en/user-guide/adapters/fastapi_nats/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,36 +121,36 @@ Alternatively, you can use [**Cookiecutter**](../../cookiecutter/index.md), whic

As shown in the [**architecture overview**](#architecture-overview), this setup requires **three** components (applications). Let's begin our code walkthrough, starting with the [**NATS App**](#3-nats-app).

#### 1. **Import Required Modules**
#### 1. **Define Workflow**

To get started, import the required modules from the **FastAgency** and **AutoGen**. These imports provide the essential building blocks for creating agents, workflows, and integrating with the client. Additionally, import the [**`NatsAdapter`**](../../../api/fastagency/adapters/nats/NatsAdapter.md) class for workflow execution.
To get started, define the workflow that your application will use. This is where you specify how the agents interact and what they do. Here's a simple example of a workflow definition:

```python hl_lines="8"
{!> docs_src/getting_started/nats_n_fastapi/main_1_nats.py [ln:1-9] !}
```python
{! docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/workflow.py [ln:1-47] !}
```

#### 2. **Define Workflow**
#### 2. **Import Required Modules**

Next, define the workflow that your application will use. This is where you specify how the agents interact and what they do. Here's a simple example of a workflow definition:
Next, import the required modules from the **FastAgency**. These imports provide the essential building blocks for integrating with the client. Additionally, import the [**`NatsAdapter`**](../../../api/fastagency/adapters/nats/NatsAdapter.md) class for workflow execution.

```python
{! docs_src/getting_started/nats_n_fastapi/main_1_nats.py [ln:11-52] !}
```python hl_lines="4"
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/deployment/main_1_nats.py [ln:1-7] !}
```

#### 3. **Configure the `NatsAdapter`**

Create an instance of the [**`NatsAdapter`**](../../../api/fastagency/adapters/nats/NatsAdapter.md) and pass your workflow to it. The adapter will handle the communication with the [**`NatsProvider`**](../../../api/fastagency/adapters/nats/NatsProvider.md) and distribute workflow execution to the workers.

```python hl_lines="5 7"
{!> docs_src/getting_started/nats_n_fastapi/main_1_nats.py [ln:55-60] !}
```python hl_lines="5"
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/deployment/main_1_nats.py [ln:9-13] !}
```

#### 4. **Define FastAgency Application**

Create a [**`NatsAdapter`**](../../../api/fastagency/adapters/nats/NatsAdapter.md) and then add it to the [**FastAPI**](https://fastapi.tiangolo.com/){target="_blank"} application using the [**lifespan parameter**](https://fastapi.tiangolo.com/advanced/events/){target="_blank"}.

```python
{!> docs_src/getting_started/nats_n_fastapi/main_1_nats.py [ln:61] !}
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/deployment/main_1_nats.py [ln:15] !}
```

#### 5. **Adapter Chaining**
Expand All @@ -162,15 +162,15 @@ Next, we set up a FastAPI service to interact with the NATS.io provider. This in
=== "Mesop"

!!! note "main_2_fastapi.py"
```python hl_lines="16-18 21-22"
{!> docs_src/getting_started/nats_n_fastapi/main_2_fastapi.py [ln:1-22] !}
```python hl_lines="15-17 19-20"
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/deployment/main_2_fastapi.py [ln:1-20] !}
```

Finally, the last component is the [**Mesop Client App**](#1-mesop-client-app), which uses the [**`MesopUI`**](../../../api/fastagency/ui/mesop/MesopUI.md) to communicate with both the user and the [**`FastAPIProvider`**](../../../api/fastagency/adapters/fastapi/FastAPIProvider.md).

!!! note "main_3_mesop.py"
```python hl_lines="7-9 11"
{!> docs_src/getting_started/nats_n_fastapi/main_3_mesop.py [ln:1-11] !}
```python hl_lines="7-9 11-15"
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/deployment/main_3_mesop.py [ln:1-15] !}
```

=== "Custom REST API and WebSocket"
Expand Down Expand Up @@ -198,8 +198,8 @@ Next, we set up a FastAPI service to interact with the NATS.io provider. This in

The [**`NatsAdapter`**](../../../api/fastagency/adapters/nats/NatsAdapter.md) requires a running NATS server. The easiest way to start the NATS server is by using [**Docker**](https://www.docker.com/){target="_blank"}. FastAgency leverages the [**JetStream**](https://docs.nats.io/nats-concepts/jetstream){target="_blank"} feature of NATS and also utilizes authentication.

```python hl_lines="1 3 6 11 17"
{!> docs_src/getting_started/nats_n_fastapi/nats-server.conf [ln:1-23]!}
```python hl_lines="1 3 8 14"
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/.devcontainer/nats_server.conf [ln:1-31]!}
```

In the above Nats configuration, we define a user called `fastagency`, and its password is read from the environment variable `FASTAGENCY_NATS_PASSWORD`. We also enable JetStream in Nats and configure Nats to serve via the appropriate ports.
Expand All @@ -211,46 +211,60 @@ Please copy and paste the following code into the same folder, using the file na
=== "Mesop"

<details>
<summary>nats-server.conf</summary>
<summary>nats_server.conf</summary>
```python
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/.devcontainer/nats_server.conf !}
```
</details>

<details>
<summary>workflow.py</summary>
```python
{!> docs_src/getting_started/nats_n_fastapi/nats-server.conf !}
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/workflow.py !}
```
</details>

<details>
<summary>main_1_nats.py</summary>
```python
{!> docs_src/getting_started/nats_n_fastapi/main_1_nats.py !}
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/deployment/main_1_nats.py !}
```
</details>

<details>
<summary>main_2_fastapi.py</summary>
```python
{!> docs_src/getting_started/nats_n_fastapi/main_2_fastapi.py !}
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/deployment/main_2_fastapi.py !}
```
</details>

<details>
<summary>main_3_mesop.py</summary>
```python
{!> docs_src/getting_started/nats_n_fastapi/main_3_mesop.py !}
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/deployment/main_3_mesop.py !}
```
</details>

=== "Custom REST API and WebSocket"

<details>
<summary>nats-server.conf</summary>
<summary>nats_server.conf</summary>
```python
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/.devcontainer/nats_server.conf !}
```
</details>

<details>
<summary>workflow.py</summary>
```python
{!> docs_src/getting_started/nats_n_fastapi/nats-server.conf !}
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/workflow.py !}
```
</details>

<details>
<summary>main_1_nats.py</summary>
```python
{!> docs_src/getting_started/nats_n_fastapi/main_1_nats.py !}
{!> docs_src/getting_started/nats_n_fastapi/my_fastagency_app/my_fastagency_app/deployment/main_1_nats.py !}
```
</details>

Expand Down Expand Up @@ -298,7 +312,7 @@ Once everything is set up, you can run your FastAgency application using the fol
- Start **NATS** Docker container:
!!! note "Terminal 1"
```
docker run -d --name nats-fastagency --rm -p 4222:4222 -p 9222:9222 -p 8222:8222 -v $(pwd)/nats-server.conf:/etc/nats/nats-server.conf -e FASTAGENCY_NATS_PASSWORD='fastagency_nats_password' nats:latest -c /etc/nats/nats-server.conf
docker run -d --name nats-fastagency --rm -p 4222:4222 -p 9222:9222 -p 8222:8222 -v $(pwd)/nats_server.conf:/etc/nats/nats_server.conf -e FASTAGENCY_NATS_PASSWORD='fastagency_nats_password' nats:latest -c /etc/nats/nats_server.conf
```

- Start **FastAPI** application that provides a conversational workflow:
Expand Down Expand Up @@ -326,7 +340,7 @@ Once everything is set up, you can run your FastAgency application using the fol
- Start **NATS** Docker container:
!!! note "Terminal 1"
```
docker run -d --name nats-fastagency --rm -p 4222:4222 -p 9222:9222 -p 8222:8222 -v $(pwd)/nats-server.conf:/etc/nats/nats-server.conf -e FASTAGENCY_NATS_PASSWORD='fastagency_nats_password' nats:latest -c /etc/nats/nats-server.conf
docker run -d --name nats-fastagency --rm -p 4222:4222 -p 9222:9222 -p 8222:8222 -v $(pwd)/nats_server.conf:/etc/nats/nats_server.conf -e FASTAGENCY_NATS_PASSWORD='fastagency_nats_password' nats:latest -c /etc/nats/nats_server.conf
```

- Start **FastAPI** application that provides a conversational workflow:
Expand Down Expand Up @@ -356,7 +370,7 @@ Once everything is set up, you can run your FastAgency application using the fol
- Start **Nats** Docker container:
!!! note "Terminal 1"
```
docker run -d --name nats-fastagency --rm -p 4222:4222 -p 9222:9222 -p 8222:8222 -v $(pwd)/nats-server.conf:/etc/nats/nats-server.conf -e FASTAGENCY_NATS_PASSWORD='fastagency_nats_password' nats:latest -c /etc/nats/nats-server.conf
docker run -d --name nats-fastagency --rm -p 4222:4222 -p 9222:9222 -p 8222:8222 -v $(pwd)/nats_server.conf:/etc/nats/nats_server.conf -e FASTAGENCY_NATS_PASSWORD='fastagency_nats_password' nats:latest -c /etc/nats/nats_server.conf
```

- Start **FastAPI** application that provides a conversational workflow:
Expand Down
Loading

0 comments on commit 9417aae

Please sign in to comment.