File size: 2,858 Bytes
07037b8
51782e8
5a9c746
07037b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
015e894
07037b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
015e894
 
 
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
# Read the data
import pandas as pd
df = pd.read_csv("./Automobile_data.csv")
df = df.drop(columns = ['normalized-losses','symboling'], axis = 1)

context_data = []
for i in range(len(df)):  # Loop over rows
    context = ""
    for j in range(10):  # Loop over the first 8 columns
        context += df.columns[j]  # Add column name
        context += ": "
        context += str(df.iloc[i][j])  # Convert value to string
        context += " "
    context_data.append(context)

from langchain_groq import ChatGroq
llm = ChatGroq(model ="llama-3.1-70b-versatile",api_key = "beza")

qa_pair = []
for i in range(len(df)):
  Question = "Given the type of  "+df['make'][i]+ " "+ df['body-style'][i][:-1]+", "" what is the price?"
  Answer   = df['price'][i]
  input = f"Instruction:\n{Question}\n\nResponse:\n{Answer}"
  qa_pair.append(input)

# from langchain_groq import ChatGroq

# llm = ChatGroq(model="llama-3.1-70b-versatile",api_key= "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")
## Embedding model!
from langchain_huggingface import HuggingFaceEmbeddings
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")

# create vector store!
from langchain_chroma import Chroma

vectorstore = Chroma(
    collection_name="car_dataset_store",
    embedding_function=embed_model,
    persist_directory="./",
)

vectorstore.get().keys()
# add data to vector nstore
vectorstore.add_texts(context_data)

query = "What is make, number of doors and fuel type?"
docs = vectorstore.similarity_search(query)
print(docs[0].page_content)

retriever = vectorstore.as_retriever()

from langchain_core.prompts import PromptTemplate

template = ("""You are a car expert.
    Use the provided context to answer the question.
    If you don't know the answer, say so. Explain your answer in detail.
    Do not discuss the context in your response; just provide the answer directly.

    Context: {context}

    Question: {question}

    Answer:""")

rag_prompt = PromptTemplate.from_template(template)

from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

rag_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | rag_prompt
    | llm
    | StrOutputParser()
)

from IPython.display import display, Markdown

response = rag_chain.invoke("What is Capital of Rwanda?")
Markdown(response)

import gradio as gr

def rag_memory_stream(text):
    partial_text = ""
    for new_text in rag_chain.stream(text):
        partial_text += new_text
        yield partial_text


title = "Real-time AI App with Groq API and LangChain to Answer car questions"
demo = gr.Interface(
    title=title,
    fn=rag_memory_stream,
    inputs="text",
    outputs="text",
    allow_flagging="never",
)

demo.launch(share=True)

if __name__ == "__main__":
    demo.launch()