Arkm20 commited on
Commit
b906e49
·
verified ·
1 Parent(s): a1ceeee

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +54 -0
main.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from fastapi import FastAPI, Request
3
+ from pydantic import BaseModel
4
+ from duckduckgo_search import DDGS
5
+ import requests
6
+ from bs4 import BeautifulSoup
7
+ from together import Together
8
+ import os
9
+
10
+ # Your Together API Key
11
+ os.environ["TOGETHER_API_KEY"] = "tgp_v1_fviFjIVOiQFgzx40nMG-q_8mrWpocP8VzWBZMLH1FbA"
12
+
13
+ app = FastAPI()
14
+ client = Together()
15
+
16
+ class Query(BaseModel):
17
+ query: str
18
+
19
+ def get_web_context(prompt: str) -> dict:
20
+ results = []
21
+ with DDGS() as ddgs:
22
+ for r in ddgs.text(prompt, max_results=3):
23
+ if r.get("href"):
24
+ results.append(r["href"])
25
+ if len(results) == 3:
26
+ break
27
+
28
+ context = ""
29
+ headers = {"User-Agent": "Mozilla/5.0"}
30
+ for url in results:
31
+ try:
32
+ res = requests.get(url, headers=headers, timeout=10)
33
+ soup = BeautifulSoup(res.content, "html.parser")
34
+ text = soup.get_text(separator=' ', strip=True)
35
+ context += f"\nFrom {url}:\n{text[:2000]}\n"
36
+ except Exception as e:
37
+ context += f"\nFrom {url}:\nError retrieving content: {e}\n"
38
+ return context.strip()
39
+
40
+ @app.post("/api/search")
41
+ async def search(q: Query):
42
+ web_data = get_web_context(q.query)
43
+ messages = [
44
+ {"role": "system", "content": "You are a helpful search assistant that reads from the following web sources to answer user questions."},
45
+ {"role": "user", "content": f"{q.query}\n\nHere is some context:\n{web_data}"}
46
+ ]
47
+ try:
48
+ response = client.chat.completions.create(
49
+ model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
50
+ messages=messages
51
+ )
52
+ return {"answer": response.choices[0].message.content}
53
+ except Exception as e:
54
+ return {"answer": f"Error: {str(e)}"}