Repository navigation
Add framework quickstart snippets to Python client examples #46
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
Open
jdrhyne
wants to merge
5
commits into
main
Choose a base branch
from
docs/framework-quickstarts-wave-b
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a910c96
Add framework quickstart snippets
jdrhyne ec5bd44
Avoid secret-like placeholders in framework examples
jdrhyne ce739b2
fix: make framework quickstarts executable
jdrhyne 2a3c6cf
chore: merge main into framework quickstarts
jdrhyne cb23f1e
ci: fix secret conditions and Windows smoke test sockets
jdrhyne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import os | ||
|
|
||
| from crewai import Agent, Crew, Task | ||
| from crewai.tools import tool | ||
| from dotenv import load_dotenv | ||
|
|
||
| from nutrient_dws import NutrientClient | ||
|
|
||
| load_dotenv() | ||
| api_key = os.getenv("NUTRIENT_API_KEY") | ||
| if not api_key: | ||
| raise SystemExit("NUTRIENT_API_KEY is required in your environment or .env file.") | ||
| if not os.getenv("OPENAI_API_KEY"): | ||
| raise SystemExit("OPENAI_API_KEY is required in your environment or .env file.") | ||
|
|
||
| client = NutrientClient(api_key=api_key) | ||
|
|
||
|
|
||
| @tool("extract_document_text") | ||
| async def extract_document_text(input_path: str) -> str: | ||
| """Extract document text with Nutrient DWS before planning further processing.""" | ||
| result = await client.extract_text(input_path) | ||
| return str(result) | ||
|
|
||
|
|
||
| document_planner = Agent( | ||
| role="Document Workflow Planner", | ||
| goal="Design a robust Nutrient DWS processing pipeline for incoming files.", | ||
| backstory="You optimize OCR, extraction, and redaction workflows for accuracy.", | ||
| tools=[extract_document_text], | ||
| llm="gpt-4.1-mini", | ||
| ) | ||
|
|
||
| document_reviewer = Agent( | ||
| role="Document QA Reviewer", | ||
| goal="Review proposed workflow plans for failure modes and compliance risks.", | ||
| backstory="You specialize in validating document automation quality.", | ||
| llm="gpt-4.1-mini", | ||
| ) | ||
|
|
||
| plan_task = Task( | ||
| description=( | ||
| "Use your extraction tool on ./assets/sample.pdf, then design a pipeline " | ||
| "that redacts emails and exports a cleaned PDF." | ||
| ), | ||
| expected_output="Extracted text and a numbered plan for the remaining processing.", | ||
| agent=document_planner, | ||
| ) | ||
|
|
||
| review_task = Task( | ||
| description=( | ||
| "Review the proposed pipeline. Flag risks and add fallback handling steps." | ||
| ), | ||
| expected_output="Risk review with concrete mitigations.", | ||
| agent=document_reviewer, | ||
| ) | ||
|
|
||
| crew = Crew( | ||
| agents=[document_planner, document_reviewer], | ||
| tasks=[plan_task, review_task], | ||
| verbose=True, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(crew.kickoff()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import asyncio | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| from dotenv import load_dotenv | ||
| from langchain_core.tools import StructuredTool | ||
| from langchain_openai import ChatOpenAI | ||
| from langgraph.prebuilt import create_react_agent | ||
|
|
||
| from nutrient_dws import NutrientClient | ||
|
|
||
| load_dotenv() | ||
| api_key = os.getenv("NUTRIENT_API_KEY") | ||
| if not api_key: | ||
| raise SystemExit("NUTRIENT_API_KEY is required in your environment or .env file.") | ||
| if not os.getenv("OPENAI_API_KEY"): | ||
| raise SystemExit("OPENAI_API_KEY is required in your environment or .env file.") | ||
|
|
||
| client = NutrientClient(api_key=api_key) | ||
|
|
||
|
|
||
| async def redact_emails(path: str) -> str: | ||
| result = await client.create_redactions_ai( | ||
| path, | ||
| criteria="Redact all email addresses.", | ||
| redaction_state="apply", | ||
| ) | ||
| output_path = Path("output") / "redacted.pdf" | ||
| output_path.parent.mkdir(parents=True, exist_ok=True) | ||
| output_path.write_bytes(result["buffer"]) | ||
| return f"Saved redacted PDF to {output_path} ({len(result['buffer'])} bytes)." | ||
|
|
||
|
|
||
| tool = StructuredTool.from_function( | ||
| coroutine=redact_emails, | ||
| name="redact_emails", | ||
| description="Redact email addresses from a document using Nutrient DWS.", | ||
| ) | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| model = ChatOpenAI( | ||
| model="gpt-4.1-mini", | ||
| api_key=os.environ["OPENAI_API_KEY"], | ||
| ) | ||
| agent = create_react_agent(model, [tool]) | ||
| state = await agent.ainvoke( | ||
| {"messages": [("user", "Redact emails from ./assets/sample.pdf.")]} | ||
| ) | ||
| print(state["messages"][-1].content) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import asyncio | ||
| import os | ||
|
|
||
| from agents import Agent, Runner, function_tool | ||
| from dotenv import load_dotenv | ||
|
|
||
| from nutrient_dws import NutrientClient | ||
|
|
||
| load_dotenv() | ||
| api_key = os.getenv("NUTRIENT_API_KEY") | ||
| if not api_key: | ||
| raise SystemExit("NUTRIENT_API_KEY is required in your environment or .env file.") | ||
| if not os.getenv("OPENAI_API_KEY"): | ||
| raise SystemExit("OPENAI_API_KEY is required in your environment or .env file.") | ||
|
|
||
| client = NutrientClient(api_key=api_key) | ||
|
|
||
|
|
||
| @function_tool | ||
| async def extract_text(input_path: str) -> str: | ||
| """Extract text from a document using Nutrient DWS.""" | ||
| result = await client.extract_text(input_path) | ||
| return str(result) | ||
|
|
||
|
|
||
| assistant = Agent( | ||
| name="nutrient-openai-agents-demo", | ||
| model="gpt-4.1-mini", | ||
| instructions="Help users extract text and summarize document workflows.", | ||
| tools=[extract_text], | ||
| ) | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| run = await Runner.run( | ||
| assistant, | ||
| "Extract text from ./assets/sample.pdf and summarize it in three bullets.", | ||
| ) | ||
| print(run.final_output) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.