Skip to content

Commit

Permalink
Docs for whatsapp tutorial (#462)
Browse files Browse the repository at this point in the history
* Initial whatsapp tutorial docs

* wip

* wip

* wip

* wip

* wip

* wip

* Update tutorial

* Update docs

* some fixes

* Fix links

---------

Co-authored-by: Davor Runje <[email protected]>
  • Loading branch information
rjambrecic and davorrunje authored Oct 22, 2024
1 parent d38a040 commit 50a365e
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 25 deletions.
11 changes: 10 additions & 1 deletion .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@
"is_secret": false
}
],
"docs/docs/en/tutorial/whatsapp/index.md": [
{
"type": "Secret Keyword",
"filename": "docs/docs/en/tutorial/whatsapp/index.md",
"hashed_secret": "52e0d11a70cdb70d4c51064d08bd8f179ebb8bb4",
"is_verified": false,
"line_number": 83
}
],
"docs/docs/en/user-guide/adapters/fastapi_nats/index.md": [
{
"type": "Secret Keyword",
Expand Down Expand Up @@ -194,5 +203,5 @@
}
]
},
"generated_at": "2024-10-18T09:09:00Z"
"generated_at": "2024-10-22T14:15:38Z"
}
3 changes: 2 additions & 1 deletion docs/docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ search:
- [Security](user-guide/api/security.md)
- [Testing](user-guide/testing/index.md)
- [CLI](user-guide/cli/index.md)
- Tutorial
- [Tutorial](tutorial/index.md)
- [Giphy API & WebSurfer](tutorial/giphy/index.md)
- [Infobip WhatsApp API & WebSurfer](tutorial/whatsapp/index.md)
- Reference
- API
- fastagency
Expand Down
16 changes: 16 additions & 0 deletions docs/docs/en/tutorial/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Web Scraping and API Integration with FastAgency

In this series of tutorials, you'll learn how to use the **FastAgency** framework to create interactive chatbots that can scrape the web and work with different APIs to respond to user requests.

## What You Will Learn

By the end of these tutorials, you’ll know how to:

- Build agents that can automatically pull information from websites.
- Connect your chatbot to external APIs to add more features.
- Set up workflows that handle conversations between users and agents.
- Keep API communication secure and manage sensitive information.

We'll guide you step-by-step through building these agents, working with APIs, and creating a chatbot that responds to user input with useful and personalized content.

Get ready to explore web scraping and API integration with **FastAgency**!
3 changes: 3 additions & 0 deletions docs/docs/en/tutorial/whatsapp/images/initial_message.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/docs/en/tutorial/whatsapp/images/scraped_info.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/docs/en/tutorial/whatsapp/images/scraping.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/docs/en/tutorial/whatsapp/images/whatsapp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/docs/en/tutorial/whatsapp/images/whatsapp_api_call.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
282 changes: 282 additions & 0 deletions docs/docs/en/tutorial/whatsapp/index.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/docs/navigation_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ search:
- [Security](user-guide/api/security.md)
- [Testing](user-guide/testing/index.md)
- [CLI](user-guide/cli/index.md)
- Tutorial
- [Tutorial](tutorial/index.md)
- [Giphy API & WebSurfer](tutorial/giphy/index.md)
- [Infobip WhatsApp API & WebSurfer](tutorial/whatsapp/index.md)
- Reference
{api}
{cli}
Expand Down
41 changes: 19 additions & 22 deletions docs/docs_src/tutorial/whatsapp/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from pathlib import Path
from typing import Annotated, Any, Optional

from autogen import register_function
Expand All @@ -22,21 +21,19 @@
"temperature": 0.8,
}

whatsapp_openapi_path = (
Path(__file__).parent / "../../../../examples/openapi/whatsapp_openapi.json"
)
with whatsapp_openapi_path.open() as f:
openapi_json = f.read()

openapi_url = "https://raw.githubusercontent.com/airtai/fastagency/refs/heads/main/examples/openapi/whatsapp_openapi.json"
whatsapp_api = OpenAPI.create(
openapi_json=openapi_json,
openapi_url=openapi_url,
)

whatsapp_api_key = "App " # pragma: allowlist secret
whatsapp_api_key += os.getenv("WHATSAPP_API_KEY", "")
whatsapp_api.set_security_params(APIKeyHeader.Parameters(value=whatsapp_api_key))
header_authorization = "App " # pragma: allowlist secret
header_authorization += os.getenv("WHATSAPP_API_KEY", "")
whatsapp_api.set_security_params(APIKeyHeader.Parameters(value=header_authorization))

WHATSAPP_SYSTEM_MESSAGE = """You are an agent in charge to communicate with the user and WhatsAPP API.
# This is the default sender number for Infobip.
# If you want to use your own sender, please update the value below:
sender = "447860099299"
WHATSAPP_SYSTEM_MESSAGE = f"""You are an agent in charge to communicate with the user and WhatsAPP API.
Always use 'present_completed_task_or_ask_question' to interact with the user.
- make sure that the 'message' parameter contains all the necessary information for the user!
Initially, the Web_Surfer_Agent will provide you with some content from the web.
Expand All @@ -45,24 +42,24 @@
- "If you want to receive the summary of the page as a WhatsApp message, please provide your number."
When sending the message, the Body must use the following format:
{
"from": "447860099299",
{{
"from": "{sender}",
"to": "receiverNumber",
"messageId": "test-message-randomInt",
"content": {
"content": {{
"text": "message"
},
}},
"callbackData": "Callback data"
}
}}
"from" number is always the same.
"""

wf = AutoGenWorkflows()


@wf.register(name="whatsapp", description="WhatsApp chat")
def whatsapp_workflow(ui: UI, params: dict[str, Any]) -> str:
@wf.register(name="whatsapp_and_websurfer", description="WhatsApp and WebSurfer chat")
def whatsapp_and_websurfer_workflow(ui: UI, params: dict[str, Any]) -> str:
def is_termination_msg(msg: dict[str, Any]) -> bool:
return msg["content"] is not None and "TERMINATE" in msg["content"]

Expand All @@ -71,15 +68,15 @@ def present_completed_task_or_ask_question(
) -> Optional[str]:
try:
return ui.text_input(
sender="whatsapp_agent",
recipient="whatsapp_agent",
sender="Whatsapp_Agent",
recipient="User",
prompt=message,
)
except Exception as e: # pragma: no cover
return f"present_completed_task_or_ask_question() FAILED! {e}"

whatsapp_agent = ConversableAgent(
name="Whatsapp_Agent",
name="WhatsApp_Agent",
system_message=WHATSAPP_SYSTEM_MESSAGE,
llm_config=llm_config,
human_input_mode="NEVER",
Expand Down

0 comments on commit 50a365e

Please sign in to comment.