Merge pull request #108 from publicarray/patch-1
chore: updated llama3.1_local_rag.py
This commit is contained in:
commit
45537ab883
2 changed files with 12 additions and 7 deletions
|
|
@ -1,15 +1,19 @@
|
||||||
import streamlit as st
|
import streamlit as st
|
||||||
import ollama
|
|
||||||
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||||
from langchain_community.document_loaders import WebBaseLoader
|
from langchain_community.document_loaders import WebBaseLoader
|
||||||
from langchain_community.vectorstores import Chroma
|
from langchain_community.vectorstores import Chroma
|
||||||
from langchain_community.embeddings import OllamaEmbeddings
|
from langchain_ollama import OllamaEmbeddings
|
||||||
|
from langchain_ollama import ChatOllama
|
||||||
|
|
||||||
st.title("Chat with Webpage 🌐")
|
st.title("Chat with Webpage 🌐")
|
||||||
st.caption("This app allows you to chat with a webpage using local llama3 and RAG")
|
st.caption("This app allows you to chat with a webpage using local llama3 and RAG")
|
||||||
|
|
||||||
# Get the webpage URL from the user
|
# Get the webpage URL from the user
|
||||||
webpage_url = st.text_input("Enter Webpage URL", type="default")
|
webpage_url = st.text_input("Enter Webpage URL", type="default")
|
||||||
|
# Connect to Ollama
|
||||||
|
ollama_endpoint = "http://127.0.0.1:11434"
|
||||||
|
ollama_model = "llama3.1"
|
||||||
|
ollama = ChatOllama(model=ollama_model, base_url=ollama_endpoint)
|
||||||
|
|
||||||
if webpage_url:
|
if webpage_url:
|
||||||
# 1. Load the data
|
# 1. Load the data
|
||||||
|
|
@ -19,14 +23,14 @@ if webpage_url:
|
||||||
splits = text_splitter.split_documents(docs)
|
splits = text_splitter.split_documents(docs)
|
||||||
|
|
||||||
# 2. Create Ollama embeddings and vector store
|
# 2. Create Ollama embeddings and vector store
|
||||||
embeddings = OllamaEmbeddings(model="llama3.1")
|
embeddings = OllamaEmbeddings(model=ollama_model, base_url=ollama_endpoint)
|
||||||
vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
|
vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
|
||||||
|
|
||||||
# 3. Call Ollama Llama3 model
|
# 3. Call Ollama Llama3 model
|
||||||
def ollama_llm(question, context):
|
def ollama_llm(question, context):
|
||||||
formatted_prompt = f"Question: {question}\n\nContext: {context}"
|
formatted_prompt = f"Question: {question}\n\nContext: {context}"
|
||||||
response = ollama.chat(model='llama3.1', messages=[{'role': 'user', 'content': formatted_prompt}])
|
response = ollama.invoke([('human', formatted_prompt)])
|
||||||
return response['message']['content']
|
return response.content.strip()
|
||||||
|
|
||||||
# 4. RAG Setup
|
# 4. RAG Setup
|
||||||
retriever = vectorstore.as_retriever()
|
retriever = vectorstore.as_retriever()
|
||||||
|
|
@ -47,4 +51,4 @@ if webpage_url:
|
||||||
# Chat with the webpage
|
# Chat with the webpage
|
||||||
if prompt:
|
if prompt:
|
||||||
result = rag_chain(prompt)
|
result = rag_chain(prompt)
|
||||||
st.write(result)
|
st.write(result)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
streamlit
|
streamlit
|
||||||
ollama
|
ollama
|
||||||
langchain
|
langchain
|
||||||
langchain_community
|
langchain_community
|
||||||
|
langchain_ollama
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue