Skip to content

Commit

Permalink
feat(docs): replaces code blocks with GithubCodeSegment object (#998)
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua Croft <[email protected]>
Co-authored-by: Joshua Croft <[email protected]>
  • Loading branch information
3 people authored Oct 17, 2024
1 parent f3ebbe6 commit d230240
Show file tree
Hide file tree
Showing 56 changed files with 7,901 additions and 5,030 deletions.
6 changes: 6 additions & 0 deletions .github/spelling/known_words_corpus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -672,3 +672,9 @@ etc
lifecycle
screenshot
docscode
richtext
bestmatches
gnews
newsrequest
v20
servicefordungeons
5 changes: 1 addition & 4 deletions pages/guides/agent-courses/agents-for-ai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ At any stage, if you encounter issues or have questions about specific terms or

Current version of the AI Engine package is <PackageVersion packageName="uagents-ai-engine" packageType="pypi" />
</Callout>

## Quick overview of Fetch.ai

Fetch.ai is developing a platform to help build an AI-enabled decentralized digital economy. Agents are
Expand Down Expand Up @@ -363,7 +363,6 @@ Let's take a look.
class SummaryRequest(Model):
url: str


SEED_PHRASE = "<your_seed_phrase>"
AGENT_MAILBOX_KEY = "<your_mailbox_key>"
OPENAI_API_KEY = "<your_open_ai_key>"
Expand All @@ -379,7 +378,6 @@ Let's take a look.
print(summaryAgent.address)
print(OPENAI_API_KEY)


@summary_protocol.on_message(model=SummaryRequest, replies={UAgentResponse})
async def summarise(ctx: Context, sender: str, msg: SummaryRequest):

Expand Down Expand Up @@ -415,7 +413,6 @@ Let's take a look.
UAgentResponse(message=(result["output_text"]), type=UAgentResponseType.FINAL),
)


summaryAgent.include(summary_protocol, publish_manifest=True)
summaryAgent.run()
```
Expand Down
5 changes: 1 addition & 4 deletions pages/guides/agent-courses/introductory-course.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ Let's then define the message handlers for the `query_proto` protocol:
We define a protocol `query_proto` using the `Protocol` class of `uagents`. It is defined with a `name` and a `version`.

The `handle_query_request()` function is the message handler function defined using the `on_message()` decorator. It handles the `QueryTableRequest` messages and replies with a `QueryTableResponse` message. The handler processes the table availability query based on the provided parameters, checks the table status stored in the agent's storage, and sends the available table numbers as a response to the querying agent.

Additionally, the handler tracks the total number of queries made and increments the count in storage. On the other hand, `handle_get_total_queries()` is the message handler function defined using the `on_query()` decorator. It handles the `GetTotalQueries` query and replies with a `TotalQueries` message containing the total number of queries made to the system. The handler retrieves the total query count from the agent's storage and responds with the count.

The overall script should look as follows:
Expand Down Expand Up @@ -1220,7 +1220,6 @@ The overall script would be:
if not completed:
await ctx.send(RESTAURANT_ADDRESS, table_query)


@user.on_message(QueryTableResponse, replies={BookTableRequest})
async def handle_query_response(ctx: Context, sender: str, msg: QueryTableResponse):
if len(msg.tables) > 0:
Expand All @@ -1236,7 +1235,6 @@ The overall script would be:
ctx.logger.info("No free tables - nothing more to do")
ctx.storage.set("completed", True)


@user.on_message(BookTableResponse, replies=set())
async def handle_book_response(ctx: Context, _sender: str, msg: BookTableResponse):
if msg.success:
Expand All @@ -1246,7 +1244,6 @@ The overall script would be:

ctx.storage.set("completed", True)


if __name__ == "__main__":
user.run()
```
Expand Down
207 changes: 123 additions & 84 deletions pages/guides/agents/advanced/agents-async-loops.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodeGroup, DocsCode } from "../../../../components/code";
import { CodeGroup, CodeSegment, DocsCode, GithubCodeSegment } from "../../../../components/code";

# Agent Asynchronous Loops

Expand Down Expand Up @@ -47,54 +47,71 @@ First of all, let's create a Python script:

Now, paste the below code into it:

```py copy filename="external_loop_attach.py"
import asyncio
import contextlib

from uagents import Agent, Bureau, Context

loop = asyncio.get_event_loop()


agent = Agent(
name="looper",
seed="<YOUR_SEED>",
)

bureau = Bureau(
agents=[agent],
)


@agent.on_event("startup")
async def startup(ctx: Context):
ctx.logger.info(">>> Looper is starting up.")


@agent.on_event("shutdown")
async def shutdown(ctx: Context):
ctx.logger.info(">>> Looper is shutting down.")


async def coro():
while True:
print("doing hard work...")
await asyncio.sleep(1)


if __name__ == "__main__":
print("Attaching the agent or bureau to the external loop...")
loop.create_task(coro())
<GithubCodeSegment digest="2f3f0744a953cd4ceff94963441a2f96">
<CodeSegment
path="https://github.com/fetchai/uAgent-Examples/blob/main/5-documentation/guides/agents/advanced/agents-async-loops/external_loop_attach.py"
lineStart={1}
lineEnd={45}
hosted={true}
/>
</GithubCodeSegment>
<CodeGroup dynamic hasCopy isLocalHostedFile digest='2f3f0744a953cd4ceff94963441a2f96'>

<DocsCode local={true}>
```py copy filename="external_loop_attach.py"

import asyncio
import contextlib

from uagents import Agent, Bureau, Context

loop = asyncio.get_event_loop()

agent = Agent(
name="looper",
seed="<YOUR_SEED>",
)

bureau = Bureau(
agents=[agent],
)


@agent.on_event("startup")
async def startup(ctx: Context):
ctx.logger.info(">>> Looper is starting up.")


@agent.on_event("shutdown")
async def shutdown(ctx: Context):
ctx.logger.info(">>> Looper is shutting down.")


async def coro():
while True:
print("doing hard work...")
await asyncio.sleep(1)


if __name__ == "__main__":
print("Attaching the agent or bureau to the external loop...")
loop.create_task(coro())

# > when attaching the agent to the external loop
loop.create_task(agent.run_async())

# > when attaching a bureau to the external loop
# loop.create_task(bureau.run_async())

with contextlib.suppress(KeyboardInterrupt):
loop.run_forever()

```
</DocsCode>

# > when attaching the agent to the external loop
loop.create_task(agent.run_async())
</CodeGroup>

# > when attaching a bureau to the external loop
# loop.create_task(bureau.run_async())

with contextlib.suppress(KeyboardInterrupt):
loop.run_forever()
```

This script is for an agent using an external event loop. We first import the required libraries for this script to be run correctly. We then proceed and instantiate an agent called `looper` using a `seed`. Remember that you need to provide a seed within the `<YOUR_SEED>` field parameter. We then need to create a `bureau` to manage the agents. We can now add the `looper` agent into the `bureau`.

Expand Down Expand Up @@ -136,47 +153,69 @@ Let's start by creating a Python script:

Then, let's paste the below code into it:

```py copy filename="external_loop_run.py"
import asyncio

from uagents import Agent, Bureau, Context

loop = asyncio.get_event_loop()

agent = Agent(
name="looper",
seed="<YOUR_SEED>",
loop=loop,
)

bureau = Bureau(
agents=[agent],
loop=loop,
)

@agent.on_event("startup")
async def startup(ctx: Context):
ctx.logger.info(">>> Looper is starting up.")

@agent.on_event("shutdown")
async def shutdown(ctx: Context):
ctx.logger.info(">>> Looper is shutting down.")

async def coro():
while True:
print("doing hard work...")
await asyncio.sleep(1)
<GithubCodeSegment digest="aa7c205c69e65b45336f9edc21defdab">
<CodeSegment
path="https://github.com/fetchai/uAgent-Examples/blob/main/5-documentation/guides/agents/advanced/agents-async-loops/external_loop_run.py"
lineStart={1}
lineEnd={43}
hosted={true}
/>
</GithubCodeSegment>
<CodeGroup dynamic hasCopy isLocalHostedFile digest='aa7c205c69e65b45336f9edc21defdab'>

<DocsCode local={true}>
```py copy filename="external_loop_run.py"

import asyncio

from uagents import Agent, Bureau, Context

loop = asyncio.get_event_loop()

agent = Agent(
name="looper",
seed="<YOUR_SEED>",
loop=loop,
)

bureau = Bureau(
agents=[agent],
loop=loop,
)


@agent.on_event("startup")
async def startup(ctx: Context):
ctx.logger.info(">>> Looper is starting up.")


@agent.on_event("shutdown")
async def shutdown(ctx: Context):
ctx.logger.info(">>> Looper is shutting down.")


async def coro():
while True:
print("doing hard work...")
await asyncio.sleep(1)


if __name__ == "__main__":
print("Starting the external loop from the agent or bureau...")
loop.create_task(coro())

# > when starting the external loop from the agent
agent.run()

# > when starting the external loop from the bureau
# bureau.run()

```
</DocsCode>

if __name__ == "__main__":
print("Starting the external loop from the agent or bureau...")
loop.create_task(coro())
</CodeGroup>

# > when starting the external loop from the agent
agent.run()

# > when starting the external loop from the bureau
# bureau.run()
```

We start by importing the required libraries to correctly run this script. We then create an asynchronous event loop using `asyncio.get_event_loop()`. This loop is used to handle all asynchronous operations, such as the agent's actions and background tasks.

Expand Down
2 changes: 1 addition & 1 deletion pages/guides/agents/advanced/dialogues.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ChitChatDialogue(Dialogue):
nodes=[ node1, node2, node3],
edges=[ init_session, start_dialogue, cont_dialogue, end_session],
)

def on_continue_dialogue(self):
return super()._on_state_transition(
cont_dialogue.name,
Expand Down
Loading

0 comments on commit d230240

Please sign in to comment.