-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
399 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from langchain_core.globals import set_llm_cache | ||
from langchain_core.caches import InMemoryCache | ||
from langchain_openai import OpenAI | ||
import time | ||
import os | ||
from dotenv import load_dotenv | ||
|
||
# Load environment variables | ||
load_dotenv() | ||
openai_api_key = os.getenv('OPENAI_API_KEY') | ||
|
||
def demonstrate_llm_caching(): | ||
# Set up the LLM | ||
llm = OpenAI(model="gpt-3.5-turbo-instruct", n=2, best_of=2) | ||
|
||
# Enable in-memory caching | ||
set_llm_cache(InMemoryCache()) | ||
|
||
# First invocation (not cached) | ||
start_time = time.time() | ||
result1 = llm.invoke("Tell me a joke") | ||
end_time = time.time() | ||
print(f"First invocation (not cached):") | ||
print(f"Result: {result1}") | ||
print(f"Time taken: {end_time - start_time:.4f} seconds\n") | ||
|
||
# Second invocation (cached) | ||
start_time = time.time() | ||
result2 = llm.invoke("Tell me a joke") | ||
end_time = time.time() | ||
print(f"Second invocation (cached):") | ||
print(f"Result: {result2}") | ||
print(f"Time taken: {end_time - start_time:.4f} seconds") | ||
|
||
|
||
# Run the demonstration | ||
demonstrate_llm_caching() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from langchain_community.cache import InMemoryCache | ||
from langchain_core.globals import set_llm_cache | ||
from langchain_openai import OpenAI | ||
import time | ||
import os | ||
from dotenv import load_dotenv | ||
|
||
# Load environment variables | ||
load_dotenv() | ||
openai_api_key = os.getenv('OPENAI_API_KEY') | ||
|
||
def demonstrate_llm_caching(): | ||
# Set up the LLM | ||
llm = OpenAI(model="gpt-3.5-turbo-instruct", n=2, best_of=2) | ||
|
||
# Enable in-memory caching | ||
set_llm_cache(InMemoryCache()) | ||
|
||
prompt = "Hello world" | ||
|
||
# Run without caching | ||
print("Running without cache:") | ||
for i in range(1, 10): | ||
result = llm.invoke(prompt) | ||
print(f"Run {i}: {result}") | ||
|
||
# Run with caching | ||
print("\nRunning with cache:") | ||
for i in range(1, 10): | ||
result = llm.invoke(prompt) | ||
print(f"Run {i}: {result}") | ||
|
||
# Run with similar prompt | ||
print("\nRunning with similar prompt:") | ||
similar_prompt = "Hello world :)" | ||
for i in range(1, 10): | ||
result = llm.invoke(similar_prompt) | ||
print(f"Similar Run {i}: {result}") | ||
|
||
# Run the demonstration | ||
demonstrate_llm_caching() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from langchain.chat_models import ChatOpenAI | ||
from langchain.tools import DuckDuckGoSearchRun | ||
from langgraph.graph import StateGraph, END | ||
from typing import TypedDict, Annotated, List | ||
import operator | ||
import os | ||
from dotenv import load_dotenv | ||
|
||
# Load environment variables | ||
load_dotenv() | ||
openai_api_key = os.getenv('OPENAI_API_KEY') | ||
|
||
# Initialize ChatOpenAI models | ||
research_model = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.7) | ||
summary_model = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.3) | ||
|
||
# Initialize search tool | ||
search_tool = DuckDuckGoSearchRun() | ||
|
||
class ResearchState(TypedDict): | ||
query: str | ||
research_results: Annotated[List[str], operator.add] | ||
summary: str | ||
|
||
graph = StateGraph(ResearchState) | ||
|
||
def research_agent(state): | ||
query = state["query"] | ||
search_result = search_tool.run(query) | ||
return {"research_results": [search_result]} | ||
|
||
def summarization_agent(state): | ||
research_results = state["research_results"] | ||
summary_prompt = f"Summarize the following research results:\n{research_results}" | ||
summary = summary_model.predict(summary_prompt) | ||
return {"summary": summary} | ||
|
||
# Add nodes | ||
graph.add_node("research", research_agent) | ||
graph.add_node("summarize", summarization_agent) | ||
|
||
# Set entry point and connect nodes | ||
graph.set_entry_point("research") | ||
graph.add_edge("research", "summarize") | ||
graph.add_edge("summarize", END) | ||
|
||
# Compile and run the graph | ||
app = graph.compile() | ||
|
||
def run_research(query): | ||
result = app.invoke({"query": query, "research_results": [], "summary": ""}) | ||
return result["summary"] | ||
|
||
# Example usage | ||
research_topic = "Latest advancements in AI" | ||
summary = run_research(research_topic) | ||
print(f"Research Summary on '{research_topic}':\n{summary}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import os | ||
import streamlit as st | ||
from dotenv import load_dotenv | ||
from litellm import completion | ||
|
||
# Load environment variables | ||
load_dotenv() | ||
openai_api_key = os.getenv('OPENAI_API_KEY') | ||
|
||
def get_model_response(model_name: str, prompt: str) -> str: | ||
response = completion(model=model_name, messages=[{"role": "user", "content": prompt}]) | ||
return response.choices[0].message.content | ||
|
||
st.title("Multi-Model Chat") | ||
|
||
# Model selection | ||
model_option = st.selectbox( | ||
"Choose a language model:", | ||
("gpt-3.5-turbo", "ollama/llama2", "gpt-4o") | ||
) | ||
|
||
# Initialize session state for chat history | ||
if 'chat_history' not in st.session_state: | ||
st.session_state['chat_history'] = [] | ||
|
||
# User input | ||
user_input = st.text_input("You:") | ||
|
||
# Send button | ||
if st.button("Send"): | ||
if openai_api_key or user_input: | ||
# Append user message to chat history | ||
st.session_state['chat_history'].append({"role": "user", "content": user_input}) | ||
|
||
# Generate response using the selected model | ||
with st.spinner("Thinking..."): | ||
response = get_model_response(model_name=model_option, prompt=user_input) | ||
|
||
# Append model response to chat history | ||
st.session_state['chat_history'].append({"role": "model", "content": response}) | ||
|
||
# Display chat history in reverse order | ||
for message in reversed(st.session_state['chat_history']): | ||
if message['role'] == 'user': | ||
st.write(f"You: {message['content']}") | ||
else: | ||
st.write(f"Model: {message['content']}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import os | ||
from dotenv import load_dotenv | ||
import streamlit as st | ||
from langchain.chat_models import ChatOpenAI | ||
from langchain.chains import ConversationalRetrievalChain | ||
from langchain.memory import ConversationBufferMemory | ||
from langchain.document_loaders import PyPDFLoader | ||
from langchain.vectorstores import Chroma | ||
from langchain.embeddings.openai import OpenAIEmbeddings | ||
from langchain.text_splitter import CharacterTextSplitter | ||
import tempfile | ||
|
||
# Load API key | ||
load_dotenv() | ||
openai_api_key = os.getenv('OPENAI_API_KEY') | ||
|
||
# Load and process PDF | ||
@st.cache_resource | ||
def load_and_process_pdf(pdf_file): | ||
# Create a temporary file | ||
with tempfile.NamedTemporaryFile(delete=False) as tmp_file: | ||
tmp_file.write(pdf_file.getvalue()) | ||
tmp_file_path = tmp_file.name | ||
|
||
loader = PyPDFLoader(tmp_file_path) | ||
documents = loader.load() | ||
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) | ||
texts = text_splitter.split_documents(documents) | ||
embeddings = OpenAIEmbeddings() | ||
vectorstore = Chroma.from_documents(texts, embeddings) | ||
|
||
# Remove the temporary file | ||
os.unlink(tmp_file_path) | ||
|
||
return vectorstore | ||
|
||
# Conversation chain | ||
def get_conversation_chain(vectorstore): | ||
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo", api_key=openai_api_key) | ||
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) | ||
conversation_chain = ConversationalRetrievalChain.from_llm( | ||
llm=llm, | ||
retriever=vectorstore.as_retriever(), | ||
memory=memory | ||
) | ||
return conversation_chain | ||
|
||
# Handle user input | ||
def handle_user_input(user_question): | ||
response = st.session_state.conversation({'question': user_question}) | ||
st.session_state.chat_history = response['chat_history'] | ||
|
||
for i, message in enumerate(st.session_state.chat_history): | ||
if i % 2 == 0: | ||
st.write("Human: " + message.content) | ||
else: | ||
st.write("AI: " + message.content) | ||
|
||
# Handle application | ||
def main(): | ||
st.set_page_config(page_title="Chat with your PDF", page_icon=":books:") | ||
st.header("Chat with your PDF:") | ||
|
||
if "conversation" not in st.session_state: | ||
st.session_state.conversation = None | ||
if "chat_history" not in st.session_state: | ||
st.session_state.chat_history = None | ||
|
||
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf") | ||
if uploaded_file is not None: | ||
with st.spinner("Processing PDF..."): | ||
vectorstore = load_and_process_pdf(uploaded_file) | ||
st.session_state.conversation = get_conversation_chain(vectorstore) | ||
st.success("PDF processed successfully!") | ||
|
||
user_question = st.text_input("Ask a question about your PDF:") | ||
if user_question: | ||
handle_user_input(user_question) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
boto3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from langchain.chat_models import ChatOpenAI | ||
from langchain.callbacks import get_openai_callback | ||
import os | ||
from dotenv import load_dotenv | ||
|
||
# Load environment variables | ||
load_dotenv() | ||
openai_api_key = os.getenv('OPENAI_API_KEY') | ||
|
||
# Initialize model | ||
llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key) | ||
|
||
with get_openai_callback() as callback: | ||
llm.invoke("What is AI?") | ||
|
||
print(callback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
from transformers import ( | ||
AutoModelForSequenceClassification, | ||
AutoTokenizer, | ||
TextClassificationPipeline, | ||
) | ||
|
||
_toxicity_model_path = "martin-ha/toxic-comment-model" | ||
|
||
model_path = _toxicity_model_path | ||
_toxicity_tokenizer = AutoTokenizer.from_pretrained(model_path) | ||
model = AutoModelForSequenceClassification.from_pretrained(model_path) | ||
_toxicity_pipeline = TextClassificationPipeline( | ||
model=model, tokenizer=_toxicity_tokenizer | ||
) | ||
|
||
text="You really suck" | ||
result = _toxicity_pipeline( | ||
text, truncation=True, max_length=_toxicity_tokenizer.model_max_length | ||
) | ||
print(result) | ||
|
||
|
||
text="I love you" | ||
result = _toxicity_pipeline( | ||
text, truncation=True, max_length=_toxicity_tokenizer.model_max_length | ||
) | ||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
from dotenv import load_dotenv | ||
from langchain_openai import ChatOpenAI | ||
from langchain.chains import LLMChain | ||
from langchain.prompts import PromptTemplate | ||
from langchain.document_loaders import WebBaseLoader | ||
from langchain.text_splitter import CharacterTextSplitter | ||
from langchain.vectorstores import Chroma | ||
from langchain.embeddings.openai import OpenAIEmbeddings | ||
|
||
# load api key | ||
load_dotenv() | ||
openai_api_key = os.getenv('OPENAI_API_KEY') | ||
|
||
def create_vectorstore(url): | ||
loader = WebBaseLoader(url) | ||
documents = loader.load() | ||
|
||
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) | ||
texts = text_splitter.split_documents(documents) | ||
|
||
embeddings = OpenAIEmbeddings() | ||
return Chroma.from_documents(texts, embeddings) | ||
|
||
def summarize_url(url): | ||
vectorstore = create_vectorstore(url) | ||
|
||
llm = ChatOpenAI(model_name="gpt-3.5-turbo", | ||
temperature=0.2, | ||
api_key=openai_api_key) | ||
|
||
prompt_template = """Write a concise summary of the following text: | ||
{text} | ||
CONCISE SUMMARY:""" | ||
|
||
prompt = PromptTemplate(template=prompt_template, input_variables=["text"]) | ||
chain = LLMChain(llm=llm, prompt=prompt) | ||
|
||
docs = vectorstore.similarity_search(query="", k=3) | ||
summaries = [] | ||
for doc in docs: | ||
summary = chain.run(text=doc.page_content) | ||
summaries.append(summary) | ||
|
||
final_summary = "\n\n".join(summaries) | ||
return final_summary | ||
|
||
# Lets go | ||
url = input("Enter a URL to summarize: ") | ||
summary = summarize_url(url) | ||
print(f"SUMMARY OF {url}:\n{summary}") |
Binary file not shown.
Oops, something went wrong.