Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 984acea

Browse filesBrowse files
Updated README, llama_cpp/llama.py, pyproject.toml
1 parent f12a228 commit 984acea
Copy full SHA for 984acea

File tree

Expand file treeCollapse file tree

3 files changed

+125
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+125
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,46 @@ For instance, if you want to work with larger contexts, you can expand the conte
649649
llm = Llama(model_path="./models/7B/llama-model.gguf", n_ctx=2048)
650650
```
651651

652+
### Cross-encoders
653+
654+
To utilise the cross-encoder, you follow the below code instructions:
655+
```
656+
- Define the location of your nltk data directory
657+
data_path1 = llama_cpp.nlLoader(nltkData="<PATH TO NLTK DATA DIRECTORY>")
658+
659+
- Initialize the sentenc-splitter function
660+
paragraphs1 = llama_cpp.sentSplit()
661+
662+
- Define the passSearch function to join sentences
663+
passages1 = llama_cpp.passSearch()
664+
665+
- Call the LlamaX class to define the model. Add the path to your Bert cross-encoder.
666+
import os
667+
username = os.getenv("USERNAME")
668+
modelPath = LlamaX(
669+
model_path=f"C:\\Users\\{username}\\Models\\cross-encoder\\ms-marco-TinyBERT-L-2",
670+
)
671+
672+
- Load LlamaCpp GGUF embeddings model and parse in the searchQuery function with the scored passages to generate embeddings
673+
username = os.getenv("USERNAME")
674+
print("")
675+
try:
676+
llm_embed = Llama(
677+
model_path=f"C:\\Users\\{username}\\Models\\mxbai\\mxbai-embed-large-v1.Q4_K_M.gguf",
678+
embedding=True,
679+
)
680+
# Store each document in a vector embedding database
681+
for i, d in enumerate(llama_cpp.searchQuery(
682+
question=question,
683+
model_path=modelPath)):
684+
response = llm_embed.create_embedding(
685+
input=d
686+
)
687+
embedding = response["data"][0]["embedding"]
688+
collection.add(ids=[str(i)], embeddings=[embedding], documents=[d])
689+
print("Processing embeddings...")
690+
```
691+
652692
## OpenAI Compatible Web Server
653693

654694
`llama-cpp-python` offers a web server which aims to act as a drop-in replacement for the OpenAI API.

‎llama_cpp/llama.py

Copy file name to clipboardExpand all lines: llama_cpp/llama.py
+83Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
import warnings
1313
import contextlib
1414
import multiprocessing
15+
from sentence_transformers import CrossEncoder
16+
from pathlib import Path
17+
from nltk import sent_tokenize
18+
import nltk
19+
import os
1520

1621
from typing import (
1722
Any,
@@ -51,6 +56,84 @@
5156
from ._logger import set_verbose
5257
from ._utils import suppress_stdout_stderr
5358

59+
# LlamaX class that takes a string as input and returns that string
60+
class LlamaX:
61+
def __init__(self, model_path: str):
62+
self.model_path = model_path
63+
64+
def get_model_path(self) -> str:
65+
return self.model_path
66+
67+
# nltk dataloader. requires nltk to installed
68+
def nlLoader(nltkData):
69+
nltk_data_dir = Path(nltkData)
70+
nltk.data.path.append(str(nltk_data_dir))
71+
72+
# Sentence-splitter function
73+
def sentSplit():
74+
75+
username = os.getenv('USERNAME')
76+
path = f'C:\\Users\\{username}\\Documents\\Data'
77+
if os.path.exists(path) == False:
78+
print('Creating new directory called `Data` in your Documents directory. Please ensure that you add your RAG ingested data to the directory as a .txt file. Thank you!')
79+
os.mkdir(f'C:\\Users\\{username}\\Documents\\Data')
80+
81+
context_dir = path #"context_dir"
82+
for filename in os.listdir(context_dir):
83+
file_path = os.path.join(context_dir, filename)
84+
if filename.endswith('.txt'):
85+
with open(file_path, encoding='utf-8') as file:
86+
document = file.read()
87+
88+
## We split this article into paragraphs and then every paragraph into sentences
89+
paragraphs = []
90+
try:
91+
for paragraph in document.replace("\r\n", "\n").split("\n\n"):
92+
if len(paragraph.strip()) > 0:
93+
paragraphs.append(sent_tokenize(paragraph.strip()))
94+
return paragraphs
95+
except Exception as e:
96+
print('''
97+
IMPORTANT NOTICE:
98+
Please add your text dataset to the Data directory. Before continuing. Thank you!''')
99+
print(e)
100+
101+
# Paragraph search function. Window-size may be adjusted
102+
def passSearch():
103+
window_size = 3
104+
passages = []
105+
for paragraph in sentSplit():
106+
for start_idx in range(0, len(paragraph), window_size):
107+
end_idx = min(start_idx + window_size, len(paragraph))
108+
passages.append(" ".join(paragraph[start_idx:end_idx]))
109+
return passages
110+
111+
# Search in a loop for individual queries and predict the scores for the [query, passage] pairs
112+
def searchQuery(question, model_path: str):
113+
query = []
114+
query.append(question)
115+
docs = []
116+
117+
for que in query:
118+
try:
119+
model = model_path
120+
# Concatenate the query and all passages and predict the scores for the pairs [query, passage]
121+
model_inputs = [[que, passage] for passage in passSearch()]
122+
scores = model.predict(model_inputs)
123+
124+
# Sort the scores in decreasing order
125+
results = [{"input": inp, "score": score} for inp, score in zip(model_inputs, scores)]
126+
results = sorted(results, key=lambda x: x["score"], reverse=True)
127+
for hit in results[0:1]:
128+
print("")
129+
docs.append(hit["input"][1])
130+
print("Performing in-document search...")
131+
print("")
132+
return docs
133+
except Exception as e:
134+
print("Oh no!, It seems that you have not added you text dataset to the Data directory.")
135+
print(e)
136+
54137

55138
class Llama:
56139
"""High-level Python wrapper for a llama.cpp model."""

‎pyproject.toml

Copy file name to clipboardExpand all lines: pyproject.toml
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ dependencies = [
1616
"numpy>=1.20.0",
1717
"diskcache>=5.6.1",
1818
"jinja2>=2.11.3",
19+
"sentence-transformers==2.7.0",
20+
"nltk==3.8.1",
1921
]
2022
requires-python = ">=3.8"
2123
classifiers = [

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.