File size: 5,454 Bytes
cefa4a2 49b450f cefa4a2 49b450f cefa4a2 ab32275 cefa4a2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# imports
import together
import os
os.environ["TOGETHER_API_KEY"] = "6e132bb99c767328701e4870bad6b3234b94ee701dbf7b995cdbec44fb01687a"
# !pip show langchain
# set your API key
together.api_key = os.environ["TOGETHER_API_KEY"]
# list available models and descriptons
models = together.Models.list()
together.Models.start("togethercomputer/llama-2-70b-chat")
import logging
from typing import Any, Dict, List, Mapping, Optional
from pydantic import Extra, Field, root_validator
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.llms.base import LLM
from langchain.llms.utils import enforce_stop_tokens
from langchain.utils import get_from_dict_or_env
class TogetherLLM(LLM):
"""Together large language models."""
model: str = "togethercomputer/llama-2-70b-chat"
"""model endpoint to use"""
together_api_key: str = os.environ["TOGETHER_API_KEY"]
"""Together API key"""
temperature: float = 0.0
"""What sampling temperature to use."""
max_tokens: int = 512
"""The maximum number of tokens to generate in the completion."""
class Config:
extra = Extra.forbid
# @root_validator()
# def validate_environment(cls, values: Dict) -> Dict:
# """Validate that the API key is set."""
# api_key = get_from_dict_or_env(
# values, "together_api_key", "TOGETHER_API_KEY"
# )
# values["together_api_key"] = api_key
# return values
@property
def _llm_type(self) -> str:
"""Return type of LLM."""
return "together"
def _call(
self,
prompt: str,
**kwargs: Any,
) -> str:
"""Call to Together endpoint."""
together.api_key = self.together_api_key
output = together.Complete.create(prompt,
model=self.model,
max_tokens=self.max_tokens,
temperature=self.temperature,
)
text = output['output']['choices'][0]['text']
return text
import os
"""# import"""
from langchain.vectorstores import Chroma
from langchain.chains import RetrievalQA
from langchain.document_loaders import DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceInstructEmbeddings
# from langchain.document_loaders import TextLoader
# from langchain.document_loaders import PyPDFLoader
from InstructorEmbedding import INSTRUCTOR
loader = DirectoryLoader('Data')
documents = loader.load()
len(documents)
#splitting the text into
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=64)
texts = text_splitter.split_documents(documents)
# HF Instructor Embeddings
from langchain.embeddings import HuggingFaceInstructEmbeddings
instructor_embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-base",
# model_kwargs={"device": "cuda"})
model_kwargs={"device": "cpu"})
"""## create the DB
This will take a bit of time on a T4 GPU
"""
persist_directory = 'db'
## Here is the nmew embeddings being used
embedding = instructor_embeddings
vectordb = Chroma.from_documents(documents=texts,
embedding=embedding,
persist_directory=persist_directory)
"""## Make a retriever"""
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
"""## Make a chain"""
llm = TogetherLLM(
model= "togethercomputer/llama-2-70b-chat",
temperature = 0.0,
max_tokens = 1024
)
DEFAULT_SYSTEM_PROMPT = """
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
""".strip()
def generate_prompt(prompt: str, system_prompt: str = DEFAULT_SYSTEM_PROMPT) -> str:
return f"""
[INST] <>
{system_prompt}
<>
{prompt} [/INST]
""".strip()
# SYSTEM_PROMPT = "Answer from following context, if question is out of context respond you don't know and do not explain the same"
SYSTEM_PROMPT = "Answer from following context, if question is out of context respond i don't know"
template = generate_prompt(
"""
{context}
Question: {question}
""",
system_prompt=SYSTEM_PROMPT,
)
print(template)
from langchain import HuggingFacePipeline, PromptTemplate
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
print(prompt)
# create the chain to answer questions
qa_chain = RetrievalQA.from_chain_type(llm=llm,
chain_type="stuff",
retriever=retriever,
return_source_documents=True,
chain_type_kwargs={"prompt": prompt})
import gradio
def greet(query):
llm_response = qa_chain(query)
return llm_response['result']
gradio.Interface(greet, "text", "text").launch() |