Skip to content

Instantly share code, notes, and snippets.

@yemiadej
Created May 27, 2024 18:33
Show Gist options
  • Save yemiadej/3ae9234c4ed80b7111960777c9cc139f to your computer and use it in GitHub Desktop.
Save yemiadej/3ae9234c4ed80b7111960777c9cc139f to your computer and use it in GitHub Desktop.
Langtrace-Langchain Integration
import os
os.environ['OPENAI_API_KEY'] = 'sk-proj-INSERT_KEY'
os.environ['LANGTRACE_API_KEY']='INSERT_KEY'
import bs4
from langtrace_python_sdk import langtrace, with_langtrace_root_span
from langfuse import Langfuse
from langfuse.openai import openai # OpenAI integration
from langchain import hub
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
#### INDEXING ####
# @with_langtrace_root_span()
def main():
# Load Documents
loader = WebBaseLoader(
web_paths=("https://lilianweng.github.io/posts/2024-02-05-human-data-quality/",),
bs_kwargs=dict(
parse_only=bs4.SoupStrainer(
class_=("post-content", "post-title", "post-header")
)
),
)
docs = loader.load()
# Split
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
# Embed
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
retriever = vectorstore.as_retriever()
#### RETRIEVAL and GENERATION ####
# Prompt
prompt = hub.pull("rlm/rag-prompt")
# LLM
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
# Post-processing
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
# Chain
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# Question
rag_chain.invoke("what is the opposite of the summary?")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment