Spaces:
Running
Running
added functions
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ import datetime
|
|
8 |
import requests
|
9 |
from dotenv import load_dotenv
|
10 |
from typing import Tuple
|
|
|
11 |
|
12 |
from pydantic import Field, BaseModel
|
13 |
from vectara_agent.agent import Agent, AgentStatusType
|
@@ -17,6 +18,14 @@ initial_prompt = "How can I help you today?"
|
|
17 |
|
18 |
load_dotenv(override=True)
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def create_tools(cfg):
|
21 |
|
22 |
class QueryHackerNews(BaseModel):
|
@@ -94,11 +103,31 @@ def create_tools(cfg):
|
|
94 |
) -> Tuple[str, str]:
|
95 |
"""
|
96 |
Get the title of a story from hacker news.
|
97 |
-
Returns
|
|
|
|
|
|
|
98 |
"""
|
99 |
db_url = 'https://hacker-news.firebaseio.com/v0/'
|
100 |
story = requests.get(f"{db_url}item/{story_id}.json").json()
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
return (
|
104 |
tools_factory.get_tools(
|
@@ -107,6 +136,7 @@ def create_tools(cfg):
|
|
107 |
get_show_stories,
|
108 |
get_ask_stories,
|
109 |
get_story_details,
|
|
|
110 |
]
|
111 |
) +
|
112 |
tools_factory.standard_tools() +
|
@@ -121,8 +151,7 @@ def initialize_agent(_cfg):
|
|
121 |
- Today's date is {date}.
|
122 |
- Never discuss politics, and always respond politely.
|
123 |
- Use tools when available instead of depending on your own knowledge.
|
124 |
-
-
|
125 |
-
0 = high hallucination, 1 = low or no hallucination. Values below 0.5 might mean the text is hallucination.
|
126 |
- If a tool cannot respond properly, retry with a rephrased question or ask the user for more information.
|
127 |
- Be very careful not to report results you are not confident about.
|
128 |
"""
|
|
|
8 |
import requests
|
9 |
from dotenv import load_dotenv
|
10 |
from typing import Tuple
|
11 |
+
from bs4 import BeautifulSoup
|
12 |
|
13 |
from pydantic import Field, BaseModel
|
14 |
from vectara_agent.agent import Agent, AgentStatusType
|
|
|
18 |
|
19 |
load_dotenv(override=True)
|
20 |
|
21 |
+
get_headers = {
|
22 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0",
|
23 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
|
24 |
+
"Accept-Language": "en-US,en;q=0.5",
|
25 |
+
"Accept-Encoding": "gzip, deflate",
|
26 |
+
"Connection": "keep-alive",
|
27 |
+
}
|
28 |
+
|
29 |
def create_tools(cfg):
|
30 |
|
31 |
class QueryHackerNews(BaseModel):
|
|
|
103 |
) -> Tuple[str, str]:
|
104 |
"""
|
105 |
Get the title of a story from hacker news.
|
106 |
+
Returns:
|
107 |
+
- The title of the story (str)
|
108 |
+
- The main URL of the story (str)
|
109 |
+
- The external link pointed to in the story (str)
|
110 |
"""
|
111 |
db_url = 'https://hacker-news.firebaseio.com/v0/'
|
112 |
story = requests.get(f"{db_url}item/{story_id}.json").json()
|
113 |
+
story_url = f'https://news.ycombinator.com/item?id={story_id}'
|
114 |
+
return story['title'], story_url, story['url'],
|
115 |
+
|
116 |
+
def get_story_text(
|
117 |
+
story_id: str = Field(..., description="The story ID.")
|
118 |
+
) -> str:
|
119 |
+
"""
|
120 |
+
Get the text of the story from hacker news (original text + all comments)
|
121 |
+
Returns the extracted text of the story as a string.
|
122 |
+
"""
|
123 |
+
url = f'https://news.ycombinator.com/item?id={story_id}'
|
124 |
+
html = requests.get(url, headers=get_headers).text
|
125 |
+
soup = BeautifulSoup(html, 'html5lib')
|
126 |
+
for element in soup.find_all(['script', 'style']):
|
127 |
+
element.decompose()
|
128 |
+
text = soup.get_text(" ", strip=True).replace('\n', ' ')
|
129 |
+
return text
|
130 |
+
|
131 |
|
132 |
return (
|
133 |
tools_factory.get_tools(
|
|
|
136 |
get_show_stories,
|
137 |
get_ask_stories,
|
138 |
get_story_details,
|
139 |
+
get_story_text,
|
140 |
]
|
141 |
) +
|
142 |
tools_factory.standard_tools() +
|
|
|
151 |
- Today's date is {date}.
|
152 |
- Never discuss politics, and always respond politely.
|
153 |
- Use tools when available instead of depending on your own knowledge.
|
154 |
+
- If a tool provides citations, you can include them in your response to provide more context.
|
|
|
155 |
- If a tool cannot respond properly, retry with a rephrased question or ask the user for more information.
|
156 |
- Be very careful not to report results you are not confident about.
|
157 |
"""
|