Skip to content

Commit

Permalink
feat: add python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
eruizgar91 committed Oct 21, 2024
1 parent ecaba18 commit 71a4d10
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 14 deletions.
8 changes: 6 additions & 2 deletions docs/libraries/02-getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ You need to keep your API Key secure and never share it with anyone.
]}>
<TabItem value="python">
```python
# ADD PYTHON CODE HERE
pip install payments-py
```
</TabItem>
<TabItem value="typescript">
Expand All @@ -62,7 +62,11 @@ You need to keep your API Key secure and never share it with anyone.
]}>
<TabItem value="python">
```python
# ADD PYTHON CODE HERE
# @see https://docs.nevermined.app/docs/tutorials/integration/nvm-api-keys
nvm_api_key = 'eyJhbGciOiJFUzI1NksifQ.ey .....'

# If you want to connect to production use 'arbitrum' as environment
payments = Payments(nvm_api_key, environment=Environment.get_environment('testing')) # 'local' | 'staging' | 'testing' | 'arbitrum' | 'peaq' | 'custom'
```
</TabItem>
<TabItem value="typescript">
Expand Down
44 changes: 39 additions & 5 deletions docs/libraries/03-assets-registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ Nevermined Payment Plans enable the set up of time-based or request-based gating
]}>
<TabItem value="python">
```python
# ADD PYTHON CODE HERE
USDC_ERC20_TESTING = '0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d' # This is the USDC ERC20 address in the testing network

plan_DID = payments_builder.create_credits_plan({
name: 'E2E Payments Plan',
description: 'description',
price:15000000n, # 15 USDC
token_address: USDC_ERC20_TESTING,
amount_of_credits: 100 # It means when someone purchase this plan, they will get 100 credits
})
```
</TabItem>
<TabItem value="typescript">
Expand Down Expand Up @@ -59,8 +67,16 @@ Nevermined Payment Plans enable the set up of time-based or request-based gating
]}>
<TabItem value="python">
```python
# ADD PYTHON CODE HERE
```
USDC_ERC20_TESTING = '0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d' # This is the USDC ERC20 address in the testing network

plan_DID = payments.create_time_plan({
name: "My 1 Month Plan",
description: "test",
price:15000000n, # 15 USDC
token_address: USDC_ERC20_TESTING,
duration: 30, # 30 days
tags: ["test"]
}) ```
</TabItem>
<TabItem value="typescript">
```typescript
Expand Down Expand Up @@ -93,8 +109,26 @@ Before registering an AI Agent, you need to have a Payment Plan created.
]}>
<TabItem value="python">
```python
# ADD PYTHON CODE HERE
```
# When you create an agent, you need to provide the endpoints that the agent exposes and are protected by the Payment Plan
# You must specify the HTTP method and the URL pattern that the agent exposes
# You can use wildcards (.*) to match any string
agent_endpoints = [
{ 'POST': 'https://example.com/api/v1/agents/(.*)/tasks' },
{ 'GET': 'https://example.com/api/v1/agents/(.*)/tasks/(.*)' }
]

agent_DID = payments_builder.create_service({
plan_DID, # The DID of the Payment Plan we created before
name: 'My AI Assistant',
description: 'description of the assistant',
serviceType: 'agent',
serviceChargeType: 'fixed',
authType: 'bearer',
token: 'changeme',
amountOfCredits: 1,
endpoints: agent_endpoints,
openEndpoints: ['https://example.com/api/v1/rest/docs-json']
}) ```
</TabItem>
<TabItem value="typescript">
```typescript
Expand Down
17 changes: 13 additions & 4 deletions docs/libraries/05-order-plans.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ With the Payments Library, Subscribers can order Plans paying in Crypto. The pro
]}>
<TabItem value="python">
```python
# ADD PYTHON CODE HERE
```
# Here we are ordering the Plan created in the previous steps
order_result = payments.order_plan(plan_DID)
# OUTPUT: orderResult:
# { success: True, agreementId: '0xaabbcc' } ```
</TabItem>
<TabItem value="typescript">
```typescript
Expand Down Expand Up @@ -50,8 +52,15 @@ Time-based Plans have a balance of 1 credit for subscribers. When the plan expir
]}>
<TabItem value="python">
```python
# ADD PYTHON CODE HERE
```
balance_result = payments.get_plan_balance(plan_DID)
# OUTPUT: balanceResult:
# {
# "planType": 'credits",
# "isSubscriptor": True,
# "isOwner": False,
# "balance": 10000000
#}
```
</TabItem>
<TabItem value="typescript">
```typescript
Expand Down
19 changes: 17 additions & 2 deletions docs/libraries/06-query-agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ Once a user is a subscriber sending a request is quite simple.
]}>
<TabItem value="python">
```python
# ADD PYTHON CODE HERE
access_config = payments.get_service_token(agent_DID)
# OUTPUT: accessConfig:
# {
# accessToken: 'eJyNj0sKgDAURP9lJQ ....',
# neverminedProxyUri: 'https://proxy.testing.nevermined.app'
# }
```
</TabItem>
<TabItem value="typescript">
Expand Down Expand Up @@ -58,7 +63,17 @@ The [Nevermined Query Protocol](https://docs.nevermined.io/docs/protocol/query-p
]}>
<TabItem value="python">
```python
# ADD PYTHON CODE HERE
# Here we are configuring the Task we are sending to the Agent. Please check the Query Protocol documentation for more information.
# https://docs.nevermined.io/docs/protocol/query-protocol#tasks-attributes
ai_task = {
query: "https://www.youtube.com/watch?v=0tZFQs7qBfQ",
name: "transcribe",
"additional_params": [],
"artifacts": []
}

task_result = payments.ai_protocol.create_task(agentDID, ai_task)

```
</TabItem>
<TabItem value="typescript">
Expand Down
18 changes: 17 additions & 1 deletion docs/libraries/08-process-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,23 @@ The AI Builders can create simple worker AI process subscribing to Nevermined to
]}>
<TabItem value="python">
```python
# ADD PYTHON CODE HERE
import asyncio

asyncio.create_task(builder.ai_protocol.subscribe(callback_function, join_account_room=True, join_agent_rooms=[], subscribe_event_types=['step-updated'], get_pending_events_on_subscribe=True))

callback_function = (step) => {
print('Step received', step)
await payments.ai_protocol.update_step(did=step['did'],
task_id=step['task_id'],
step_id=step['step_id'],
step={'step_id': step['step_id'],
'task_id': step['task_id'],
'step_status': AgentExecutionStatus.Completed.value,
'output': 'success',
'is_last': True,
'cost': 1
})
}
```
</TabItem>
<TabItem value="typescript">
Expand Down

0 comments on commit 71a4d10

Please sign in to comment.