Spaces:
Sleeping
Sleeping
File size: 7,611 Bytes
ecb6e4b 14a9422 ecb6e4b 57077ba ecb6e4b f92f6b6 ecb6e4b 57077ba ecb6e4b 3032452 ecb6e4b 3032452 ecb6e4b 3032452 ecb6e4b 57077ba ecb6e4b f92f6b6 ecb6e4b 9b43a21 f92f6b6 ecb6e4b 0ff1ebd ecb6e4b f92f6b6 ecb6e4b 57cc9b4 ecb6e4b 9b43a21 ecb6e4b 0ff1ebd ecb6e4b a117ff7 ecb6e4b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
import os
from omegaconf import OmegaConf
import requests
from typing import Tuple
from bs4 import BeautifulSoup
from dotenv import load_dotenv
load_dotenv(override=True)
from pydantic import Field, BaseModel
from vectara_agentic.agent import Agent
from vectara_agentic.tools import ToolsFactory, VectaraToolFactory
from vectara_agentic.tools_catalog import summarize_text
initial_prompt = "How can I help you today?"
get_headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
}
def create_assistant_tools(cfg):
class QueryHackerNews(BaseModel):
query: str = Field(..., description="The user query.")
vec_factory = VectaraToolFactory(vectara_api_key=cfg.api_key,
vectara_customer_id=cfg.customer_id,
vectara_corpus_id=cfg.corpus_id)
summarizer = 'vectara-experimental-summary-ext-2023-12-11-med-omni'
ask_hackernews = vec_factory.create_rag_tool(
tool_name = "ask_hackernews",
tool_description = """
Provides information on any topic or query, based on relevant hacker news stories.
""",
tool_args_schema = QueryHackerNews,
reranker = "multilingual_reranker_v1", rerank_k = 100,
n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
summary_num_results = 10,
vectara_summarizer = summarizer,
include_citations = True,
)
def get_top_stories(
n_stories: int = Field(default=10, description="The number of top stories to return.")
) -> list[str]:
"""
Get the top stories from hacker news.
Returns a list of story IDS for the top stories right now. These are the top stories on hacker news.
"""
db_url = 'https://hacker-news.firebaseio.com/v0/'
top_stories = requests.get(f"{db_url}topstories.json").json()
return top_stories[:n_stories]
def get_show_stories(
n_stories: int = Field(default=10, description="The number of top SHOW HN stories to return.")
) -> list[str]:
"""
Get the top SHOW HN stories from hacker news.
Returns a list of story IDS for the top SHOW HN stories right now. These are stories where users show their projects.
"""
db_url = 'https://hacker-news.firebaseio.com/v0/'
top_stories = requests.get(f"{db_url}showstories.json").json()
return top_stories[:n_stories]
def get_ask_stories(
n_stories: int = Field(default=10, description="The number of top ASK HN stories to return.")
) -> list[str]:
"""
Get the top ASK HN stories from hacker news.
Returns a list of story IDS for the top ASK HN stories right now. These are stories where users ask questions to the community.
"""
db_url = 'https://hacker-news.firebaseio.com/v0/'
top_stories = requests.get(f"{db_url}askstories.json").json()
return top_stories[:n_stories]
def get_story_details(
story_id: str = Field(..., description="The story ID.")
) -> Tuple[str, str]:
"""
Get the title, url and external link of a story from hacker news.
Returns:
- The title of the story (str)
- The main URL of the story (str)
- The external link pointed to in the story (str)
- The author of the story
- The number of descendants (comments + replies) of the story
"""
db_url = 'https://hacker-news.firebaseio.com/v0/'
story = requests.get(f"{db_url}item/{story_id}.json").json()
story_url = f'https://news.ycombinator.com/item?id={story_id}'
return story['title'], story_url, story['url'], story['by'], story['descendants']
def get_story_text(
story_id: str = Field(..., description="The story ID.")
) -> str:
"""
Get the text of the story from hacker news (original text + all comments)
Returns the extracted text of the story as a string.
"""
url = f'https://news.ycombinator.com/item?id={story_id}'
html = requests.get(url, headers=get_headers).text
soup = BeautifulSoup(html, 'html5lib')
for element in soup.find_all(['script', 'style']):
element.decompose()
text = soup.get_text(" ", strip=True).replace('\n', ' ')
return text
def whats_new(
n_stories: int = Field(default=10, description="The number of new stories to return.")
) -> list[str]:
"""
Provides a succint summary of what is new in the hackernews community
by summarizing the content and comments of top stories.
Returns a string with the summary.
"""
stories = get_top_stories(n_stories)
texts = [get_story_text(story_id) for story_id in stories[:n_stories]]
all_stories = '---------\n\n'.join(texts)
return summarize_text(all_stories)
tools_factory = ToolsFactory()
return (
[ask_hackernews] +
[tools_factory.create_tool(tool) for tool in
[
get_top_stories,
get_show_stories,
get_ask_stories,
get_story_details,
get_story_text,
whats_new,
]
] +
tools_factory.get_llama_index_tools(
"tavily_research", "TavilyToolSpec",
tool_name_prefix="tavily", api_key=cfg.tavily_api_key
) +
tools_factory.standard_tools()
)
def initialize_agent(_cfg, agent_progress_callback = None):
bot_instructions = """
- You are a helpful assistant, with expertise in answering user questions based on Hacker News stories and comments.
- Always call the ask_hackernews tool first as your primary source of information. Then try other tools.
- Give slight preference to newer stories when answering questions.
- when possible, include links to Hacker News stories. Use the actual title of the story as the link's displayed text.
Don't use text like "Source" which doesn't tell the user what the link is about.
- Don't include external links in your responses unless the user asks for them.
- You can use the tavily_search tool to gain additional information if needed for follow up questions about Hacker News topic.
- When including information or links provided by the tavily_search tool, make sure to notify the user in your response that this is not based on Hacker News stories.
"""
agent = Agent(
tools=create_assistant_tools(_cfg),
topic="hacker news",
custom_instructions=bot_instructions,
agent_progress_callback=agent_progress_callback
)
agent.report()
return agent
def get_agent_config() -> OmegaConf:
cfg = OmegaConf.create({
'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
'api_key': str(os.environ['VECTARA_API_KEY']),
'examples': os.environ.get('QUERY_EXAMPLES', None),
'demo_name': "hacker-news-chat",
'demo_welcome': "Welcome to the Hacker News Assistant demo.",
'demo_description': "This demo can be used to ask about Hacker News.",
'tavily_api_key': str(os.environ['TAVILY_API_KEY']),
})
return cfg
|