mirror of
https://github.com/neuml/txtai.git
synced 2026-10-11 21:39:55 +00:00
26 KiB
26 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[agent] model2vecIn [ ]:
from datetime import datetime
from txtai import Agent
wikipedia = {
"name": "wikipedia",
"description": "Searches a Wikipedia database",
"provider": "huggingface-hub",
"container": "neuml/txtai-wikipedia"
}
arxiv = {
"name": "arxiv",
"description": "Searches a database of scientific papers",
"provider": "huggingface-hub",
"container": "neuml/txtai-arxiv"
}
def today() -> str:
"""
Gets the current date and time
Returns:
current date and time
"""
return datetime.today().isoformat()
agent = Agent(
tools=[today, wikipedia, arxiv, "websearch"],
llm="Qwen/Qwen3-4B-Instruct-2507",
max_iterations=10,
)In [3]:
agent("Which city has the highest population, Boston or New York?", maxlength=16000)Out [3]:
[32;20;1m======== New task ========[0m
[37;1mWhich city has the highest population, Boston or New York?[0m
[33;1m=== Agent thoughts:[0m
[0mThought: I will use the tool 'web_search' to find the population of both cities.[0m
[33;1m>>> Calling tool: 'web_search' with arguments: {'query': 'Population of Boston and New York'}[0m
[33;1m=== Agent thoughts:[0m
[0mThought: The results from the web search indicate that the population of Boston is 7,948 and the population of New York is 653,833. However, it seems that the question is asking about the city of Boston in New York, not the city of Boston in Massachusetts. I will use the tool 'wikipedia' to find the population of the city of New York.[0m
[33;1m>>> Calling tool: 'wikipedia' with arguments: {'query': 'Population of New York City'}[0m
[33;1m=== Agent thoughts:[0m
[0mThought: The results from the Wikipedia search indicate that the population of New York City is approximately 8,804,190. However, this is the population of the entire city, not just the city of New York. I will use the tool 'wikipedia' to find the population of the city of New York, not the state of New York.[0m
[33;1m>>> Calling tool: 'wikipedia' with arguments: {'query': 'Population of New York City (borough)'}[0m
[33;1m=== Agent thoughts:[0m
[0mThought: The results from the Wikipedia search indicate that the population of New York City is approximately 8,804,190, and the population of Brooklyn is approximately 2,592,149. Since Brooklyn is one of the five boroughs of New York City, I will assume that the population of the city of New York is approximately 8,804,190. However, this is still not the correct answer to the original question. I will use the tool 'wikipedia' to find the population of the city of Boston in Massachusetts, which is the other city mentioned in the original question.[0m
[33;1m>>> Calling tool: 'wikipedia' with arguments: {'query': 'Population of Boston, Massachusetts'}[0m
[33;1m=== Agent thoughts:[0m
[0mThought: The results from the Wikipedia search indicate that the population of Boston, Massachusetts is approximately 675,647. This is the correct answer to the original question, which was to determine which city has the highest population, Boston or New York. Since the population of Boston is significantly lower than the population of New York City, which is approximately 8,804,190, the answer to the original question is New York City.[0m
[33;1m>>> Calling tool: 'final_answer' with arguments: {'answer': 'New York City'}[0m
'New York City'
In [8]:
from IPython.display import display, Markdown
researcher = """
You're an expert researcher looking to write a paper on {topic}.
Search for websites, scientific papers and Wikipedia related to the topic.
Write a report with summaries and references (with hyperlinks).
Write the text as Markdown.
"""
display(Markdown(agent(researcher.format(topic="alien life"))))[32;20;1m======== New task ========[0m
[37;1m
You're an expert researcher looking to write a paper on alien life.
Search for websites, scientific papers and Wikipedia related to the topic.
Write a report with summaries and references (with hyperlinks).
Write the text as Markdown.
[0m
[33;1m=== Agent thoughts:[0m
[0mThought: I will start by searching for Wikipedia articles related to alien life.[0m
[33;1m>>> Calling tool: 'wikipedia' with arguments: {'query': 'Alien life'}[0m
[33;1m=== Agent thoughts:[0m
[0mThought: The search results from Wikipedia look promising. I will now search for scientific papers related to alien life using the arxiv tool.[0m
[33;1m>>> Calling tool: 'arxiv' with arguments: {'query': 'alien life'}[0m
[33;1m=== Agent thoughts:[0m
[0mThought: The search results from arxiv look interesting. I will now search for websites related to alien life using the web_search tool.[0m
[33;1m>>> Calling tool: 'web_search' with arguments: {'query': 'alien life'}[0m
[33;1m=== Agent thoughts:[0m
[0mThought: Now that I have gathered information from Wikipedia, arxiv, and web search, I will write a report with summaries and references (with hyperlinks) in Markdown format.[0m
[33;1m>>> Calling tool: 'final_answer' with arguments: {'answer': "## Alien Life: A Report on the Search for Extraterrestrial Life\n\n### Introduction\n\nThe search for alien life has been ongoing for centuries, with scientists and enthusiasts alike searching for signs of life beyond Earth. While no conclusive evidence has been found yet, there are many promising leads and discoveries that suggest the possibility of life existing elsewhere in the universe.\n\n### Wikipedia\n\nAccording to Wikipedia, extraterrestrial life refers to life that does not originate from Earth. The search for alien life is an active area of research, with scientists using various methods to detect signs of life, such as analyzing the atmospheres of exoplanets and searching for biosignatures.\n\n### Arxiv\n\nThe arxiv database has numerous papers on the search for alien life, including studies on the possibility of life existing on exoplanets and the use of astrobiology to detect signs of life. One paper, 'Extraterrestrial Life and Censorship,' discusses the development of the theory of cosmic life and the attempts to censor evidence incompatible with this theory.\n\n### Web Search\n\nA web search for alien life yielded numerous results, including articles from reputable sources such as NASA and Live Science. These articles discuss the latest discoveries and findings in the search for alien life, including the potential for life existing on exoplanets and the use of astrobiology to detect signs of life.\n\n### Conclusion\n\nWhile the search for alien life is an ongoing and active area of research, there are many promising leads and discoveries that suggest the possibility of life existing elsewhere in the universe. As scientists continue to search for signs of life, it is possible that we may one day find evidence of alien life.\n\n### References\n\n* [Extraterrestrial life - Wikipedia](https://en.wikipedia.org/wiki/Extraterrestrial_life)\n* [What's the best evidence we've found for alien life?](https://www.livescience.com/space/extraterrestrial-life/whats-the-best-evidence-weve-found-for-alien-life)\n* [NASA Research Gives Guideline for Future Alien Life Search](https://www.nasa.gov/universe/nasa-research-gives-guideline-for-future-alien-life-search/)\n* [James Webb telescope sees potential signs of alien life in the atmosphere of a distant goldilocks water-world](https://www.livescience.com/space/exoplanets/james-webb-telescope-sees-potential-signs-of-alien-life-in-the-atmosphere-of-a-distant-goldilocks-water-world)\n* [Alien life in Universe: Scientists say finding it is 'only a matter of time'](https://www.bbc.com/news/science-environment-66950930)\n* [Extraterrestrial evidence: 10 incredible findings about aliens from 2020](https://www.livescience.com/alien-discoveries-2020.html)\n* [How Scientists Could Tell the World if They Find Alien Life](https://www.scientificamerican.com/article/how-scientists-could-tell-the-world-if-they-find-alien-life/)"}[0m
<IPython.core.display.Markdown object>
In [ ]:
from txtai import Agent, LLM
# Share the LLM instance across multiple agents
llm = LLM("Qwen/Qwen3-4B-Instruct-2507")
websearcher = Agent(
tools=["websearch"],
llm=llm,
)
wikiman = Agent(
tools=[{
"name": "wikipedia",
"description": "Searches a Wikipedia database",
"provider": "huggingface-hub",
"container": "neuml/txtai-wikipedia"
}],
llm=llm,
)
researcher = Agent(
tools=[{
"name": "arxiv",
"description": "Searches a database of scientific papers",
"provider": "huggingface-hub",
"container": "neuml/txtai-arxiv"
}],
llm=llm,
)
agent = Agent(
tools=[{
"name": "websearcher",
"description": "I run web searches, there is no answer a web search can't solve!",
"target": websearcher
}, {
"name": "wikiman",
"description": "Wikipedia has all the answers, I search Wikipedia and answer questions",
"target": wikiman
}, {
"name": "researcher",
"description": "I'm a science guy. I search arXiv to get all my answers.",
"target": researcher
}],
llm=llm,
max_iterations=10
)In [20]:
display(Markdown(agent("""
Work with your team and build a comprehensive report on fundamental concepts about Signal Processing.
Write the output in Markdown.
""", maxlength=16000)))<IPython.core.display.Markdown object>
In [9]:
%%writefile config.yml
agent:
researcher:
tools:
- websearch
- name: wikipedia
description: Searches a Wikipedia database
provider: huggingface-hub
container: neuml/txtai-wikipedia
- name: arxiv
description: Searches a database of scientific papers
provider: huggingface-hub
container: neuml/txtai-arxiv
llm:
path: Qwen/Qwen3-4B-Instruct-2507Writing config.yml
In [ ]:
!CONFIG=config.yml nohup uvicorn "txtai.api:app" &> api.log &
!sleep 90In [ ]:
import requests
requests.post(
"http://localhost:8000/agent",
headers={"Content-Type": "application/json"},
json={"name": "researcher", "text": "Tell me about the Roman Empire", "maxlength": 160000}
).json()'The Roman Empire was a vast and powerful state that existed from 27 BC to 476 AD, covering much of Europe, North Africa, and Western Asia. It was founded by Augustus Caesar and was ruled by a series of emperors, with its capital in Rome. The Roman Empire was known for its impressive architecture, engineering, and administrative achievements, including the construction of roads, bridges, and public buildings. It also had a significant impact on the development of law, governance, and culture in the ancient world.'
In [1]:
from txtai import Embeddings
# Data to index
data = [
"US tops 5 million confirmed virus cases",
"Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg",
"Beijing mobilises invasion craft along coast as Taiwan tensions escalate",
"The National Park Service warns against sacrificing slower friends in a bear attack",
"Maine man wins $1M from $25 lottery ticket",
"Make huge profits without work, earn up to $100,000 a day"
]
# Create an embeddings
embeddings = Embeddings(method="model2vec", path="minishlab/M2V_base_output")
embeddings.index(data)
uid = embeddings.search("climate change")[0][0]
data[uid]Out [1]:
"Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg"