Update token_counter.py
Browse files- token_counter.py +47 -11
token_counter.py
CHANGED
@@ -1,15 +1,51 @@
|
|
1 |
-
|
2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
class TokenCounterTool(Tool):
|
5 |
-
name = "text_generator"
|
6 |
-
description = "This is a tool for counting token used by a prompt. It takes a prompt as input and returns the generated text."
|
7 |
inputs = ["text"]
|
8 |
outputs = ["text"]
|
9 |
|
10 |
-
def __call__(self,
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain.document_loaders import TextLoader
|
3 |
+
from langchain.vectorstores import Chroma
|
4 |
+
from langchain.chains import RetrievalQA
|
5 |
+
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
6 |
+
from langchain.agents import Tool
|
7 |
+
|
8 |
+
# Initialize the HuggingFaceInstructEmbeddings
|
9 |
+
hf = HuggingFaceInstructEmbeddings(
|
10 |
+
model_name="hkunlp/instructor-large",
|
11 |
+
embed_instruction="Represent the document for retrieval: ",
|
12 |
+
query_instruction="Represent the query for retrieval: "
|
13 |
+
)
|
14 |
+
|
15 |
+
# Example texts for the vector store
|
16 |
+
texts=["The meaning of life is to love","The meaning of vacation is to relax","Roses are red.","Hack the planet!"]
|
17 |
+
|
18 |
+
# Create a Chroma vector store from the example texts
|
19 |
+
db = Chroma.from_texts(texts, hf, collection_name="my-collection")
|
20 |
+
|
21 |
+
# Create a RetrievalQA chain
|
22 |
+
llm = LLM.from_model("vicuna-13b") # Replace with the appropriate LLM model
|
23 |
+
docsearcher = RetrievalQA.from_chain_type(
|
24 |
+
llm=llm,
|
25 |
+
chain_type="stuff", # Replace with the appropriate chain type
|
26 |
+
return_source_documents=False,
|
27 |
+
retriever=db.as_retriever(search_type="similarity", search_kwargs={"k": 1})
|
28 |
+
)
|
29 |
+
|
30 |
+
class VectorStoreRetrieverTool(Tool):
|
31 |
+
name = "vectorstore_retriever"
|
32 |
+
description = "This tool uses LangChain's RetrievalQA to find relevant answers from a vector store based on a given query."
|
33 |
|
|
|
|
|
|
|
34 |
inputs = ["text"]
|
35 |
outputs = ["text"]
|
36 |
|
37 |
+
def __call__(self, query: str):
|
38 |
+
# Run the query through the RetrievalQA chain
|
39 |
+
response = docsearcher.run(query)
|
40 |
+
return response
|
41 |
+
|
42 |
+
# Create the Gradio interface using the HuggingFaceTool
|
43 |
+
tool = gr.Interface(
|
44 |
+
VectorStoreRetrieverTool(),
|
45 |
+
live=True,
|
46 |
+
title="LangChain-Application: Vectorstore-Retriever",
|
47 |
+
description="This tool uses LangChain's RetrievalQA to find relevant answers from a vector store based on a given query.",
|
48 |
+
)
|
49 |
+
|
50 |
+
# Launch the Gradio interface
|
51 |
+
tool.launch()
|