Skip to content

Commit

Permalink
fix(huggingface): fix not working default tool
Browse files Browse the repository at this point in the history
  • Loading branch information
David Kristek authored and David Kristek committed May 9, 2024
1 parent 33ac922 commit 1e9acde
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions examples/extensions/huggingface/huggingface_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
Follow the `pytorch issue <https://github.com/pytorch/pytorch/issues/110436>`_ for more information.
"""

import requests
from dotenv import load_dotenv
from transformers import Tool

from genai import Client
from genai.credentials import Credentials
Expand All @@ -24,6 +26,22 @@ def heading(text: str) -> str:

load_dotenv()


class BitcoinPriceFetcher(Tool):
name = "bitcoin_price_fetcher"
description = "This tool fetches the current price of Bitcoin in USD."
inputs = []
outputs = ["text"]

def __call__(self):
try:
response = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
data = response.json()
return str(data["bpi"]["USD"]["rate"])
except Exception:
return "Unable to fetch the current price of Bitcoin."


client = Client(credentials=Credentials.from_env())

print(heading("Transformers Agent"))
Expand All @@ -33,7 +51,7 @@ def heading(text: str) -> str:
client=client,
model="meta-llama/llama-2-70b-chat",
parameters=TextGenerationParameters(min_new_tokens=10, max_new_tokens=200, random_seed=777, temperature=0),
additional_tools=[BitcoinPriceFetcher()],
)

agent.chat("Extract text from the given url", url="https://research.ibm.com/blog/analog-ai-chip-low-power")
agent.chat("Do the text summarization on the downloaded text.")
agent.chat("What is the current price of Bitcoin?")

0 comments on commit 1e9acde

Please sign in to comment.