Skip to content

Commit 471080a

Browse files
committed
first commit
0 parents  commit 471080a

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
model/
2+
__pycache__/

‎Dockerfile‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM python:3.11.9-slim-bookworm
2+
3+
WORKDIR /app
4+
ADD . /app
5+
6+
RUN apt update && apt install -y gcc g++
7+
8+
RUN python3 -m pip cache purge
9+
RUN python3 -m pip install --no-cache-dir -r requirements.txt
10+
RUN python3 -m nltk.downloader "punkt"
11+
RUN python3 -m nltk.downloader "stopwords"
12+
13+
EXPOSE 8501
14+
15+
ENTRYPOINT [ "streamlit", "run" ]
16+
CMD [ "app.py" ]

‎app.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import streamlit as st
2+
from websearching import web_search
3+
from llama_cpp_inf import run_inference_lcpp
4+
5+
def reply(query):
6+
jsonstr = web_search(query)
7+
results = run_inference_lcpp(jsonstr, query)
8+
return results
9+
10+
st.set_page_config(page_title="SearchPhi", page_icon="🔎")
11+
# Title of the web app
12+
st.title("SearchPhi🔎")
13+
st.subheader("With llama.cpp!🦙")
14+
# Input text box for the search query
15+
query = st.text_input("Enter search term:")
16+
17+
# Number of results to display
18+
num_results = st.number_input("Number of results to display:", min_value=1, max_value=5, value=3)
19+
20+
# Button to initiate search
21+
if st.button("Search"):
22+
if query:
23+
results = reply(query)
24+
st.write(f"**Results for '{query}':**")
25+
st.write_stream(results)
26+
else:
27+
st.write("Please enter a search term.")

‎llama_cpp_inf.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Imports
2+
from llama_cpp import Llama
3+
import re
4+
5+
## Instantiate model from downloaded file
6+
llm = Llama(
7+
model_path="model/Phi-3-mini-4k-instruct-q4.gguf",
8+
n_ctx=4096, # Context length to use
9+
n_threads=14, # Number of CPU threads to use
10+
n_gpu_layers=3 # Number of model layers to offload to GPU
11+
)
12+
13+
## Generation kwargs
14+
generation_kwargs = {
15+
"max_tokens":1024,
16+
"stop":["<|end|>"],
17+
"echo":False, # Echo the prompt in the output
18+
"top_k":1 # This is essentially greedy decoding, since the model will always return the highest-probability token. Set this value > 1 for sampling decoding
19+
}
20+
21+
def run_inference_lcpp(jsonstr, user_search):
22+
prompt = f"""Instructions for the assistant: Starting from the URLs and the keywords deriving from Google search results and provided to you in JSON format, generate a meaningful summary of the search results that satisfies the user's query.
23+
URLs and keywords in JSON format: {jsonstr}.
24+
User's query to satisfy: {user_search}"""
25+
res = llm(prompt, **generation_kwargs)
26+
response = res["choices"][0]["text"]
27+
jsondict = eval(jsonstr)
28+
addon = "Reference websites:\n- "+ '\n- '.join(list(jsondict.keys()))
29+
input_string = response.replace("<|assistant|>", "") + "\n\n" + addon
30+
frag_res = re.findall(r'\w+|\s+|[^\w\s]', input_string)
31+
for word in frag_res:
32+
yield word
33+
34+
if __name__ == "__main__":
35+
prompt = """Context: A vector database, vector store or vector search engine is a database that can store vectors (fixed-length lists of numbers) along with other data items. Vector databases typically implement one or more Approximate Nearest Neighbor (ANN) algorithms,[1][2] so that one can search the database with a query vector to retrieve the closest matching database records.
36+
37+
Vectors are mathematical representations of data in a high-dimensional space. In this space, each dimension corresponds to a feature of the data, with the number of dimensions ranging from a few hundred to tens of thousands, depending on the complexity of the data being represented. A vector's position in this space represents its characteristics. Words, phrases, or entire documents, as well as images, audio, and other types of data, can all be vectorized; Prompt: Describe what is a vector database"""
38+
res = llm(prompt, **generation_kwargs) # Res is a dictionary
39+
40+
## Unpack and the generated text from the LLM response dictionary and print it
41+
print(res["choices"][0]["text"])
42+
# res is short for result

‎requirements.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
llama_cpp_python==0.2.83
2+
streamlit==1.37.0
3+
googlesearch-python==1.2.4
4+
nltk==3.8.1
5+
rake_nltk==1.0.6
6+
boilerpy3==1.0.7

‎websearching.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from googlesearch import search
2+
from rake_nltk import Rake
3+
from boilerpy3 import extractors
4+
import json
5+
6+
extractor = extractors.ArticleExtractor()
7+
r = Rake()
8+
9+
# Function to perform web search
10+
def web_search(query, num_results=5):
11+
search_results = []
12+
for url in search(query, num_results=num_results):
13+
search_results.append(url)
14+
urls = list(set(search_results))
15+
jsonlike = {}
16+
for url in urls:
17+
try:
18+
content = extractor.get_content_from_url(url)
19+
r.extract_keywords_from_text(content)
20+
keywords = r.get_ranked_phrases()[:20]
21+
jsonlike.update({url: {"keywords": keywords}})
22+
except Exception:
23+
continue
24+
jsonstr = json.dumps(jsonlike)
25+
return jsonstr
26+
27+
28+
29+
30+

0 commit comments

Comments
 (0)