Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-chat-with-pdf-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
pratrivedi authored Feb 28, 2024
2 parents 516fac6 + ee93210 commit 0794412
Show file tree
Hide file tree
Showing 13 changed files with 2,993 additions and 19 deletions.
55 changes: 36 additions & 19 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
tests:
name: Tests
needs: check-if-files-changed
if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}

defaults:
run:
Expand All @@ -45,38 +44,46 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- uses: snok/install-poetry@v1
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
version: 1.3.2

- name: Load cached venv
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
- if: ${{
needs.check-if-files-changed.outputs.uagents == 'true' &&
steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
}}
name: Install dependencies
run: poetry install --no-interaction --no-root
- run: poetry install --no-interaction
- name: Run tests
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
run: poetry install --no-interaction
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
name: Run tests
run: |
source $VENV
poetry run pytest
linting:
name: Lint & Formatting
needs: check-if-files-changed
if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}

defaults:
run:
Expand All @@ -88,28 +95,38 @@ jobs:
python-version: [ "3.10" ]

steps:
- uses: actions/checkout@v3
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- uses: snok/install-poetry@v1
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true

- name: Load cached venv
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v2
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
- if: ${{
needs.check-if-files-changed.outputs.uagents == 'true' &&
steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
}}
name: Install dependencies
run: poetry install -E all --no-interaction --no-root

- run: poetry install -E all --no-interaction
- run: poetry run black --check .
- run: poetry run pylint $(git ls-files '*.py')
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
run: poetry install -E all --no-interaction
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
run: poetry run black --check .
- if: ${{ needs.check-if-files-changed.outputs.uagents == 'true' }}
run: poetry run pylint $(git ls-files '*.py')
27 changes: 27 additions & 0 deletions integrations/agent-negotiation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Agent based Negotiation

This example is two locally running agents Alice and Bob.

Periodically, Alice will start a negotiation with Bob. Bob will respond with a counter offer. This will continue until a deal is reached or the negotiation times out.

This serves as an simple example of how to use the `Agent` class to create a simple negotiation.

## Running the agents

Install dependencies

```bash
poetry install
```

Enter the poetry shell

```bash
poetry shell
```

Start the agents

```bash
python3 main.py
```
11 changes: 11 additions & 0 deletions integrations/agent-negotiation/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from uagents import Bureau

from negotiation.bob import bob
from negotiation.alice import alice

bureau = Bureau()
bureau.add(alice)
bureau.add(bob)

if __name__ == '__main__':
bureau.run()
Empty file.
61 changes: 61 additions & 0 deletions integrations/agent-negotiation/negotiation/alice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from uuid import uuid4

from uagents import Agent, Context

from negotiation.messages import CounterProposal, Acceptance, Reject, Proposal

ALICE_TARGET_MIN_PRICE = 60.0
ALICE_TARGET_MAX_PRICE = 110.0
ALICE_TARGET_ITEM = 'widget'

alice = Agent(name='alice', seed='alices super secret seed phrase')
print('Alice address: ', alice.address)

bob_address = 'agent1qg5a9zvex0gy2amagpvadp6f9kcf8jxa3akrqd09lf8kk2frlxj7q7pu3yt'



@alice.on_message(Acceptance)
async def handle_acceptance(ctx: Context, sender: str, msg: Acceptance):
ctx.logger.info(f'({msg.proposal_id}) accepted at price {msg.price}')


@alice.on_message(Reject)
async def handle_reject(ctx: Context, sender: str, msg: Reject):
ctx.logger.info(f'({msg.proposal_id}) rejected because {msg.reason}')


@alice.on_message(CounterProposal)
async def handle_counter_proposal(ctx: Context, sender: str, msg: CounterProposal):
ctx.logger.info(f'({msg.proposal_id}) counter offer for {msg.price}')

# evaluate the counter proposal
next_price = ((msg.price - ALICE_TARGET_MIN_PRICE) // 2) + ALICE_TARGET_MIN_PRICE

# attempt to negotiate down
await ctx.send(
bob_address,
Proposal(
id=uuid4(),
item=ALICE_TARGET_ITEM,
price=next_price,
)
)


@alice.on_interval(10)
async def on_interval(ctx: Context):
proposal_id = uuid4()
starting_price = ALICE_TARGET_MIN_PRICE

ctx.logger.info(f'({proposal_id}) proposing at price {starting_price}')

# send the proposal to Bob
await ctx.send(
bob_address,
Proposal(
id=proposal_id,
item=ALICE_TARGET_ITEM,
price=starting_price
)
)
31 changes: 31 additions & 0 deletions integrations/agent-negotiation/negotiation/bob.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from uagents import Agent, Context

from negotiation.messages import Proposal, CounterProposal, Acceptance, Reject

BOB_TARGET_MIN_PRICE = 100.0
BOB_TARGET_MAX_PRICE = 150.0
BOB_TARGET_ITEM = 'widget'

bob = Agent(name='bob', seed='bobs super secret seed phrase')
print('Bob address: ', bob.address)


@bob.on_message(Proposal)
async def handle_proposal(ctx: Context, sender: str, msg: Proposal):
ctx.logger.info(f'({msg.id}) proposal at price {msg.price}')

# if the proposal is not for the target item then it will be rejected
if msg.item != BOB_TARGET_ITEM:
await ctx.send(sender, Reject(proposal_id=msg.id, reason='I am not interested in that item'))
return

# sanity check
assert msg.item == BOB_TARGET_ITEM

# if the price is right then accept the proposal
if BOB_TARGET_MIN_PRICE <= msg.price:
await ctx.send(sender, Acceptance(proposal_id=msg.id, item=msg.item, price=msg.price))
return

# if the price is not right then counter with a proposal
await ctx.send(sender, CounterProposal(proposal_id=msg.id, item=msg.item, price=BOB_TARGET_MAX_PRICE))
25 changes: 25 additions & 0 deletions integrations/agent-negotiation/negotiation/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pydantic import UUID4
from uagents import Model


class Proposal(Model):
id: UUID4
item: str
price: float


class Reject(Model):
proposal_id: UUID4
reason: str


class CounterProposal(Model):
proposal_id: UUID4
item: str
price: float


class Acceptance(Model):
proposal_id: UUID4
item: str
price: float
Loading

0 comments on commit 0794412

Please sign in to comment.