File size: 2,199 Bytes
2b10881
 
f56fa6a
 
 
 
 
 
 
 
 
 
 
2b10881
f56fa6a
 
 
2b10881
 
f56fa6a
21f297b
f56fa6a
2b10881
f56fa6a
 
 
2b10881
 
f56fa6a
 
 
2b10881
f56fa6a
2b10881
f56fa6a
 
 
 
 
 
 
2b10881
f56fa6a
 
 
 
 
 
 
 
2b10881
f56fa6a
 
 
 
 
 
 
2b10881
 
f56fa6a
 
 
 
 
 
 
631c3cb
f56fa6a
2b10881
f56fa6a
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
import gradio as gr
from huggingface_hub import InferenceClient
import os
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
from operator import itemgetter
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.vectorstores import FAISS
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain_together import TogetherEmbeddings
from langchain_community.llms import Together


#os
os.environ["TOGETHER_API_KEY"] = "6216ce36aadcb06c35436e7d6bbbc18b354d8140f6e805db485d70ecff4481d0"


#load
loader = TextLoader("Resume_data.txt")
documents = loader.load()

# split it into chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)


vectorstore = FAISS.from_documents(docs,
     TogetherEmbeddings(model="togethercomputer/m2-bert-80M-8k-retrieval")
)

retriever = vectorstore.as_retriever()

model = Together(
    model="mistralai/Mixtral-8x7B-Instruct-v0.1",
    temperature=0.0,
    max_tokens=500,
    top_k=0,
    # together_api_key="..."
)


# Provide a template following the LLM's original chat template.
template = """<s>[INST] answer from context only as a person. and always answer in short answer.
answer for asked question only, if he greets greet back.

{context}

Question: {question} [/INST] 
"""
prompt = ChatPromptTemplate.from_template(template) 

chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | model
    | StrOutputParser()
)

def greet(query1,history):
  try:
    return chain.invoke(query1)
  except:
    return "Hi"

# gradio
description = "This is a chatbot application based on the Mixtral-8x7B model. Simply type an input to get started with chatting.\n Note : Bot can generate random response sometimes"
examples = [["what is your contact number?"], ["where you are currently working?"]]

gr.ChatInterface(greet,title = "Chat with my Bot", description=description,examples=examples).launch(debug = True)