File size: 2,829 Bytes
a004f09
 
 
 
 
 
 
 
a795478
 
 
 
e3965cf
 
a004f09
 
 
 
 
 
a795478
 
a004f09
a795478
a004f09
a795478
 
a004f09
e3965cf
 
 
a795478
 
a004f09
 
 
a795478
 
a004f09
 
 
 
 
 
a795478
a004f09
 
a795478
 
a004f09
a795478
 
a004f09
a795478
a004f09
a795478
a004f09
 
a795478
a004f09
 
 
 
 
 
 
 
a795478
a004f09
a795478
a004f09
 
 
 
a795478
a004f09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a795478
a004f09
 
a795478
a004f09
 
a795478
 
a004f09
a795478
a004f09
 
 
 
a795478
a004f09
a795478
a004f09
 
 
 
 
 
 
 
 
a795478
a004f09
a795478
 
a004f09
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
try:
  from langchain_community.vectorstores import Chroma
except:
  from langchain_community.vectorstores import Chroma


from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain import PromptTemplate
from langchain_core.prompts import ChatPromptTemplate
from langchain_groq import ChatGroq
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings
import torch






import os
import requests  # Or your Groq library

groq_api_key = os.environ.get("my_groq_api_key")

# Initialize a ChatGroq object with a temperature of 0 and the "mixtral-8x7b-32768" model.
llm = ChatGroq(temperature=0, model_name="llama3-70b-8192",api_key=groq_api_key)

model_name = "BAAI/bge-m3"
device = "cuda" if torch.cuda.is_available() else "cpu"
embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs={'device': device})
# we run this cell every time
db = Chroma(embedding_function=embeddings, persist_directory='/Persian Chroma/')



memory = ConversationBufferWindowMemory(
    memory_key="history", k=3, return_only_outputs=True
)




template = """
محتوای زیر بین انسان و هوش مصنوعی است. براساس این مکالمه به سوال مطرح شده جواب بده


محتوا:
{history}

"""
s="""

\n سوال: {input}

\n جواب:""".strip()


prompt = PromptTemplate(input_variables=["history", "input"], template=template+context_text+'\n'+s)






chain = ConversationChain(
    llm=llm,

    prompt=prompt,
    memory=memory,
    verbose=True,
)



# Generate a response from the Llama model
def get_llama_response(message: str, history: list) -> str:
    """
    Generates a conversational response from the Llama model.

    Parameters:
        message (str): User's input message.
        history (list): Past conversation history.

    Returns:
        str: Generated response from the Llama model.
    """
    query_text =message

    results = db.similarity_search_with_relevance_scores(query_text, k=2)
    context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])


    

    template = """
    محتوای زیر بین انسان و هوش مصنوعی است. براساس این مکالمه به سوال مطرح شده جواب بده


    محتوا:
    {history}

    """
    s="""
    \n سوال: {input}

    \n جواب:""".strip()
    prompt = PromptTemplate(input_variables=["history", "input"], template=template+context_text+'\n'+s)

    #print(template)
    chain.prompt=prompt
    res = chain.predict(input=query_text)
    return res
        #return response.strip()



import gradio as gr
iface = gr.Interface(fn=get_llama_response, inputs=gr.Textbox(),
             outputs="textbox")
iface.launch(share=True)