diff --git a/llm/diffbot_llm.py b/llm/diffbot_llm.py
new file mode 100644
index 0000000..f9ec4cd
--- /dev/null
+++ b/llm/diffbot_llm.py
@@ -0,0 +1,440 @@
+# /// script
+# requires-python = ">=3.11"
+# dependencies = [
+# "langchain-core==0.3.63",
+# "langchain-openai==0.3.18",
+# "marimo",
+# "openai==1.82.1",
+# ]
+# ///
+import marimo
+
+__generated_with = "0.13.7"
+app = marimo.App()
+
+
+@app.cell(hide_code=True)
+def _(mo):
+ mo.md(
+ r"""
+ ## Getting started with Diffbot LLM
+
+ ```
+ touch .env # Create a .env file
+ echo "DIFFBOT_API_TOKEN=your-token-here" >> .env # Add your token to a .env file
+ ```
+ ### Your first query with citations
+ """
+ )
+ return
+
+
+@app.cell
+def _():
+ import os
+
+ from openai import OpenAI
+
+ DIFFBOT_API_TOKEN = os.getenv("DIFFBOT_API_TOKEN")
+
+ diffbot_client = OpenAI(
+ base_url="https://llm.diffbot.com/rag/v1",
+ api_key=DIFFBOT_API_TOKEN,
+ )
+
+ completion = diffbot_client.chat.completions.create(
+ model="diffbot-small-xl",
+ messages=[{"role": "user", "content": "What is GraphRAG?"}],
+ )
+
+ print(completion.choices[0].message.content)
+ return DIFFBOT_API_TOKEN, OpenAI
+
+
+@app.cell
+def _(DIFFBOT_API_TOKEN, OpenAI):
+ def query_model(query_text, model, base_url=None, api_key=None):
+ client_args = {}
+ if base_url:
+ client_args["base_url"] = base_url
+ if api_key:
+ client_args["api_key"] = api_key
+
+ client = OpenAI(**client_args)
+ return client.chat.completions.create(
+ model=model,
+ messages=[{"role": "user", "content": query_text}],
+ )
+
+
+ def query_diffbot(query_text, model="diffbot-small-xl"):
+ return query_model(
+ query_text,
+ model=model,
+ base_url="https://llm.diffbot.com/rag/v1",
+ api_key=DIFFBOT_API_TOKEN,
+ )
+
+
+ def query_openai(query_text, model="o4-mini"):
+ return query_model(query_text, model=model)
+
+
+ def print_response(response):
+ print(response.choices[0].message.content)
+ return print_response, query_diffbot, query_openai
+
+
+@app.cell
+def _(print_response, query_diffbot):
+ diffbot_completion = query_diffbot("What is GraphRAG?")
+ print_response(diffbot_completion)
+ return
+
+
+@app.cell
+def _(print_response, query_openai):
+ openai_completion = query_openai("What is GraphRAG?")
+ print_response(openai_completion)
+ return
+
+
+@app.cell
+def _(print_response, query_diffbot):
+ diffbot_completion_1 = query_diffbot("What is the weather in Tokyo?")
+ print_response(diffbot_completion_1)
+ return
+
+
+@app.cell
+def _(print_response, query_openai):
+ openai_completion_1 = query_openai("What is the weather in Tokyo?")
+ print_response(openai_completion_1)
+ return
+
+
+@app.cell
+def _(print_response, query_diffbot):
+ diffbot_completion_2 = query_diffbot(
+ "Find me the information on the upcoming hackathon organized by HuggingFace"
+ )
+ print_response(diffbot_completion_2)
+ return
+
+
+@app.cell
+def _(print_response, query_openai):
+ completion_openai_2 = query_openai("Find me the information on the upcoming hackathon organized by HuggingFace")
+
+ print_response(completion_openai_2)
+ return
+
+
+@app.cell
+def _(print_response, query_diffbot):
+ diffbot_completion_3 = query_diffbot("Find the square root of 12394890235")
+ print_response(diffbot_completion_3)
+ return
+
+
+@app.cell
+def _(print_response, query_openai):
+ openai_completion_3 = query_openai("Find the square root of 12394890235")
+ print_response(openai_completion_3)
+ return
+
+
+@app.cell
+def _(print_response, query_diffbot):
+ _image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Black_hole_-_Messier_87_crop_max_res.jpg/960px-Black_hole_-_Messier_87_crop_max_res.jpg"
+
+ diffbot_completion_4 = query_diffbot(f"Describe this image to me: {_image_url}")
+ print_response(diffbot_completion_4)
+ return
+
+
+@app.cell
+def _(print_response, query_openai):
+ _image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Black_hole_-_Messier_87_crop_max_res.jpg/960px-Black_hole_-_Messier_87_crop_max_res.jpg"
+
+ openai_completion_4 = query_openai(f"Describe this image to me: {_image_url}")
+ print_response(openai_completion_4)
+ return
+
+
+@app.cell
+def _(query_diffbot):
+ _image_url = "https://codecut.ai/wp-content/uploads/2025/05/codecut-home-image.png"
+ diffbot_completion_5 = query_diffbot(f"Describe this image to me: {_image_url}")
+ print(diffbot_completion_5.choices[0].message.content)
+ return
+
+
+@app.cell
+def _(query_openai):
+ _image_url = "https://codecut.ai/wp-content/uploads/2025/05/codecut-home-image.png"
+
+ openai_completion_5 = query_openai(f"Describe this image to me: {_image_url}")
+ print(openai_completion_5.choices[0].message.content)
+ return
+
+
+@app.cell(hide_code=True)
+def _(mo):
+ mo.md(r"""### Self-hosting for privacy""")
+ return
+
+
+@app.cell(hide_code=True)
+def _(mo):
+ mo.md(
+ r"""
+ If your use-case involves high-stakes sensitive information like financial or medical databases, you can get all the benefits of the Serverless API locally by running a couple of Docker commands:
+
+ For the 8B model, much smaller in disk size:
+
+ ```bash
+ docker run --runtime nvidia --gpus all -p 8001:8001 --ipc=host -e VLLM_OPTIONS="--model diffbot/Llama-3.1-Diffbot-Small-2412 --served-model-name diffbot-small --enable-prefix-caching" docker.io/diffbot/diffbot-llm-inference:latest
+ ```
+
+ For the larger 70B model with full capabilities:
+
+ ```bash
+ docker run --runtime nvidia --gpus all -p 8001:8001 --ipc=host -e VLLM_OPTIONS="--model diffbot/Llama-3.3-Diffbot-Small-XL-2412 --served-model-name diffbot-small-xl --enable-prefix-caching --quantization fp8 --tensor-parallel-size 2" docker.io/diffbot/diffbot-llm-inference:latest
+ ```
+
+ Once the application starts up successfully and you see a message like the following:
+
+ ```plaintext
+ INFO: Application startup complete.
+ INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
+ ```
+
+ You can run all the examples above by replacing the base URL with the endpoint `http://localhost:8001/rag/v1`.
+
+ However, do note that these models require high-end GPUs like A100 and H100s to run at full precision. If you don't have the right hardware, consider using [RunPod.io](https://runpod.io) which cost:
+
+ - $5.98/hr for dual H100 GPU setup (total 160 GB VRAM)
+ - $1.89/hr for a single A100 GPU setup (80 GB VRAM)
+
+ 
+ """
+ )
+ return
+
+
+@app.cell(hide_code=True)
+def _(mo):
+ mo.md(
+ r"""
+ ## Building real-world applications with Diffbot and LangChain
+
+ ### LangChain + Diffbot basics
+ """
+ )
+ return
+
+
+@app.cell
+def _(DIFFBOT_API_TOKEN):
+ from langchain_openai import ChatOpenAI
+
+ llm = ChatOpenAI(
+ model="diffbot-small-xl",
+ temperature=0,
+ max_tokens=None,
+ timeout=None,
+ base_url="https://llm.diffbot.com/rag/v1",
+ api_key=DIFFBOT_API_TOKEN,
+ )
+ return ChatOpenAI, llm
+
+
+@app.cell
+def _(llm):
+ messages = [
+ ("system", "You are a data scientist who writes efficient Python code."),
+ ("human", "Given a DataFrame with columns 'product' and 'sales', calculates the total sales for each product."),
+ ]
+
+ ai_msg = llm.invoke(messages)
+ print(ai_msg.content)
+ return
+
+
+@app.cell
+def _(llm):
+ from langchain_core.prompts import ChatPromptTemplate
+
+ prompt = ChatPromptTemplate.from_messages(
+ [
+ (
+ "system",
+ "You are a data scientist who writes efficient {language} code",
+ ),
+ ("human", "{input}"),
+ ]
+ )
+ chain = prompt | llm
+ _result = chain.invoke(
+ {
+ "language": "SQL",
+ "input": "Given a table with columns 'product' and 'sales', calculates the total sales for each product.",
+ }
+ )
+ print(_result.content)
+ return (ChatPromptTemplate,)
+
+
+@app.cell(hide_code=True)
+def _(mo):
+ mo.md(r"""### Building a RAG application with Diffbot and LangChain""")
+ return
+
+
+@app.cell
+def _(ChatOpenAI, ChatPromptTemplate):
+ import json
+ from typing import Dict, List
+
+ from langchain_core.output_parsers import StrOutputParser
+
+ class ResearchAssistant:
+
+ def __init__(self, diffbot_api_key: str):
+ self.llm = ChatOpenAI(
+ model="diffbot-small-xl",
+ temperature=0.3,
+ base_url="https://llm.diffbot.com/rag/v1",
+ api_key=diffbot_api_key,
+ )
+ self.setup_chains()
+
+ def setup_chains(self):
+ self.topic_extraction_prompt = ChatPromptTemplate.from_template(
+ """
+ Analyze the following document and extract 3-5 main topics or entities that would benefit
+ from current information. Return as a JSON list of topics.
+
+ Document: {document}
+
+ Topics (JSON format):
+ """
+ )
+ self.research_prompt = ChatPromptTemplate.from_template(
+ """
+ Provide comprehensive, current information about: {topic}
+
+ Context from document: {context}
+
+ Include:
+ 1. Current status and recent developments
+ 2. Key statistics or data points
+ 3. Recent news or updates
+ 4. Relevant industry trends
+
+ Ensure all facts are cited with sources.
+ """
+ )
+ self.report_prompt = ChatPromptTemplate.from_template(
+ """
+ Create a comprehensive research report based on the document analysis and current research.
+
+ Original Document Summary: {document_summary}
+
+ Research Findings: {research_findings}
+
+ Generate a well-structured report that:
+ 1. Summarizes the original document's main points
+ 2. Provides current context for each major topic
+ 3. Identifies any outdated information in the document
+ 4. Suggests areas for further investigation
+
+ Include proper citations throughout.
+ """
+ )
+
+ def extract_topics(self, document: str) -> List[str]:
+ """Extract main topics from the document for research."""
+ chain = self.topic_extraction_prompt | self.llm | StrOutputParser()
+ try:
+ _result = chain.invoke({"document": document})
+ topics = json.loads(_result.strip())
+ return topics if isinstance(topics, list) else []
+ except (json.JSONDecodeError, Exception) as e:
+ print(f"Error extracting topics: {e}")
+ return []
+
+ def research_topic(self, topic: str, context: str) -> str:
+ """Research current information about a specific topic."""
+ chain = self.research_prompt | self.llm | StrOutputParser()
+ return chain.invoke({"topic": topic, "context": context})
+
+ def generate_report(self, document: str, research_findings: List[Dict]) -> str:
+ """Generate comprehensive report with current information."""
+ summary_prompt = ChatPromptTemplate.from_template(
+ "Provide a concise summary of this document: {document}"
+ )
+ summary_chain = summary_prompt | self.llm | StrOutputParser()
+ document_summary = summary_chain.invoke({"document": document})
+ findings_text = "\n\n".join(
+ [
+ f"**{finding['topic']}:**\n{finding['research']}"
+ for finding in research_findings
+ ]
+ )
+ report_chain = self.report_prompt | self.llm | StrOutputParser()
+ return report_chain.invoke(
+ {
+ "document_summary": document_summary,
+ "research_findings": findings_text,
+ }
+ )
+
+ def analyze_document(self, document: str) -> Dict:
+ """Complete document analysis with current research."""
+ print("Extracting topics from document...")
+ topics = self.extract_topics(document)
+ if not topics:
+ return {"error": "Could not extract topics from document"}
+ print(f"Researching {len(topics)} topics...")
+ research_findings = []
+ for topic in topics:
+ print(f" - Researching: {topic}")
+ research = self.research_topic(topic, document)
+ research_findings.append({"topic": topic, "research": research})
+ print("Generating comprehensive report...")
+ final_report = self.generate_report(document, research_findings)
+ return {
+ "topics": topics,
+ "research_findings": research_findings,
+ "final_report": final_report,
+ "status": "completed",
+ }
+
+ return (ResearchAssistant,)
+
+
+@app.cell
+def _(DIFFBOT_API_TOKEN, ResearchAssistant):
+ assistant = ResearchAssistant(DIFFBOT_API_TOKEN)
+ sample_document = """
+ Artificial Intelligence has made significant progress in natural language processing.
+ Companies like OpenAI and Google have released powerful language models.
+ The field of machine learning continues to evolve with new architectures and techniques.
+ Investment in AI startups reached $25 billion in 2023.
+ """
+ _result = assistant.analyze_document(sample_document)
+ print(_result["final_report"])
+ return
+
+
+@app.cell
+def _():
+ import marimo as mo
+
+ return (mo,)
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/public/index.html b/public/index.html
index 4418751..b537353 100644
--- a/public/index.html
+++ b/public/index.html
@@ -85,6 +85,10 @@
polars vs pandas
pyspark parametrize
View the notebook
+
+ diffbot llm
+ View the notebook
+
lchain deepseek
View the notebook
diff --git a/public/llm/diffbot_llm.html b/public/llm/diffbot_llm.html
new file mode 100644
index 0000000..b073304
--- /dev/null
+++ b/public/llm/diffbot_llm.html
@@ -0,0 +1,82 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ diffbot_llm.py
+
+
+
+
+
+ diffbot llm
+
+
+
+
+
+
+
+
+
+ import%20marimo%0A%0A__generated_with%20%3D%20%220.13.7%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Getting%20started%20with%20Diffbot%20LLM%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20touch%20.env%20%20%23%20Create%20a%20.env%20file%0A%20%20%20%20echo%20%22DIFFBOT_API_TOKEN%3Dyour-token-here%22%20%3E%3E%20.env%20%20%23%20Add%20your%20token%20to%20a%20.env%20file%0A%20%20%20%20%60%60%60%0A%20%20%20%20%23%23%23%20Your%20first%20query%20with%20citations%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20os%0A%0A%20%20%20%20from%20openai%20import%20OpenAI%0A%0A%20%20%20%20DIFFBOT_API_TOKEN%20%3D%20os.getenv(%22DIFFBOT_API_TOKEN%22)%0A%0A%20%20%20%20diffbot_client%20%3D%20OpenAI(%0A%20%20%20%20%20%20%20%20base_url%3D%22https%3A%2F%2Fllm.diffbot.com%2Frag%2Fv1%22%2C%0A%20%20%20%20%20%20%20%20api_key%3DDIFFBOT_API_TOKEN%2C%0A%20%20%20%20)%0A%0A%20%20%20%20completion%20%3D%20diffbot_client.chat.completions.create(%0A%20%20%20%20%20%20%20%20model%3D%22diffbot-small-xl%22%2C%0A%20%20%20%20%20%20%20%20messages%3D%5B%7B%22role%22%3A%20%22user%22%2C%20%22content%22%3A%20%22What%20is%20GraphRAG%3F%22%7D%5D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20print(completion.choices%5B0%5D.message.content)%0A%20%20%20%20return%20DIFFBOT_API_TOKEN%2C%20OpenAI%0A%0A%0A%40app.cell%0Adef%20_(DIFFBOT_API_TOKEN%2C%20OpenAI)%3A%0A%20%20%20%20def%20query_model(query_text%2C%20model%2C%20base_url%3DNone%2C%20api_key%3DNone)%3A%0A%20%20%20%20%20%20%20%20client_args%20%3D%20%7B%7D%0A%20%20%20%20%20%20%20%20if%20base_url%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20client_args%5B%22base_url%22%5D%20%3D%20base_url%0A%20%20%20%20%20%20%20%20if%20api_key%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20client_args%5B%22api_key%22%5D%20%3D%20api_key%0A%0A%20%20%20%20%20%20%20%20client%20%3D%20OpenAI(**client_args)%0A%20%20%20%20%20%20%20%20return%20client.chat.completions.create(%0A%20%20%20%20%20%20%20%20%20%20%20%20model%3Dmodel%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20messages%3D%5B%7B%22role%22%3A%20%22user%22%2C%20%22content%22%3A%20query_text%7D%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%20%20%20%20def%20query_diffbot(query_text%2C%20model%3D%22diffbot-small-xl%22)%3A%0A%20%20%20%20%20%20%20%20return%20query_model(%0A%20%20%20%20%20%20%20%20%20%20%20%20query_text%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20model%3Dmodel%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20base_url%3D%22https%3A%2F%2Fllm.diffbot.com%2Frag%2Fv1%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20api_key%3DDIFFBOT_API_TOKEN%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%20%20%20%20def%20query_openai(query_text%2C%20model%3D%22o4-mini%22)%3A%0A%20%20%20%20%20%20%20%20return%20query_model(query_text%2C%20model%3Dmodel)%0A%0A%0A%20%20%20%20def%20print_response(response)%3A%0A%20%20%20%20%20%20%20%20print(response.choices%5B0%5D.message.content)%0A%20%20%20%20return%20print_response%2C%20query_diffbot%2C%20query_openai%0A%0A%0A%40app.cell%0Adef%20_(print_response%2C%20query_diffbot)%3A%0A%20%20%20%20diffbot_completion%20%3D%20query_diffbot(%22What%20is%20GraphRAG%3F%22)%0A%20%20%20%20print_response(diffbot_completion)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(print_response%2C%20query_openai)%3A%0A%20%20%20%20openai_completion%20%3D%20query_openai(%22What%20is%20GraphRAG%3F%22)%0A%20%20%20%20print_response(openai_completion)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(print_response%2C%20query_diffbot)%3A%0A%20%20%20%20diffbot_completion_1%20%3D%20query_diffbot(%22What%20is%20the%20weather%20in%20Tokyo%3F%22)%0A%20%20%20%20print_response(diffbot_completion_1)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(print_response%2C%20query_openai)%3A%0A%20%20%20%20openai_completion_1%20%3D%20query_openai(%22What%20is%20the%20weather%20in%20Tokyo%3F%22)%0A%20%20%20%20print_response(openai_completion_1)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(print_response%2C%20query_diffbot)%3A%0A%20%20%20%20diffbot_completion_2%20%3D%20query_diffbot(%0A%20%20%20%20%20%20%20%20%22Find%20me%20the%20information%20on%20the%20upcoming%20hackathon%20organized%20by%20HuggingFace%22%0A%20%20%20%20)%0A%20%20%20%20print_response(diffbot_completion_2)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(print_response%2C%20query_openai)%3A%0A%20%20%20%20completion_openai_2%20%3D%20query_openai(%22Find%20me%20the%20information%20on%20the%20upcoming%20hackathon%20organized%20by%20HuggingFace%22)%0A%0A%20%20%20%20print_response(completion_openai_2)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(print_response%2C%20query_diffbot)%3A%0A%20%20%20%20diffbot_completion_3%20%3D%20query_diffbot(%22Find%20the%20square%20root%20of%2012394890235%22)%0A%20%20%20%20print_response(diffbot_completion_3)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(print_response%2C%20query_openai)%3A%0A%20%20%20%20openai_completion_3%20%3D%20query_openai(%22Find%20the%20square%20root%20of%2012394890235%22)%0A%20%20%20%20print_response(openai_completion_3)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(print_response%2C%20query_diffbot)%3A%0A%20%20%20%20_image_url%20%3D%20%22https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F4%2F4f%2FBlack_hole_-_Messier_87_crop_max_res.jpg%2F960px-Black_hole_-_Messier_87_crop_max_res.jpg%22%0A%0A%20%20%20%20diffbot_completion_4%20%3D%20query_diffbot(f%22Describe%20this%20image%20to%20me%3A%20%7B_image_url%7D%22)%0A%20%20%20%20print_response(diffbot_completion_4)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(print_response%2C%20query_openai)%3A%0A%20%20%20%20_image_url%20%3D%20%22https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F4%2F4f%2FBlack_hole_-_Messier_87_crop_max_res.jpg%2F960px-Black_hole_-_Messier_87_crop_max_res.jpg%22%0A%0A%20%20%20%20openai_completion_4%20%3D%20query_openai(f%22Describe%20this%20image%20to%20me%3A%20%7B_image_url%7D%22)%0A%20%20%20%20print_response(openai_completion_4)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(query_diffbot)%3A%0A%20%20%20%20_image_url%20%3D%20%22https%3A%2F%2Fcodecut.ai%2Fwp-content%2Fuploads%2F2025%2F05%2Fcodecut-home-image.png%22%0A%20%20%20%20diffbot_completion_5%20%3D%20query_diffbot(f%22Describe%20this%20image%20to%20me%3A%20%7B_image_url%7D%22)%0A%20%20%20%20print(diffbot_completion_5.choices%5B0%5D.message.content)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(query_openai)%3A%0A%20%20%20%20_image_url%20%3D%20%22https%3A%2F%2Fcodecut.ai%2Fwp-content%2Fuploads%2F2025%2F05%2Fcodecut-home-image.png%22%0A%0A%20%20%20%20openai_completion_5%20%3D%20query_openai(f%22Describe%20this%20image%20to%20me%3A%20%7B_image_url%7D%22)%0A%20%20%20%20print(openai_completion_5.choices%5B0%5D.message.content)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%23%20Self-hosting%20for%20privacy%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20If%20your%20use-case%20involves%20high-stakes%20sensitive%20information%20like%20financial%20or%20medical%20databases%2C%20you%20can%20get%20all%20the%20benefits%20of%20the%20Serverless%20API%20locally%20by%20running%20a%20couple%20of%20Docker%20commands%3A%0A%0A%20%20%20%20For%20the%208B%20model%2C%20much%20smaller%20in%20disk%20size%3A%0A%0A%20%20%20%20%60%60%60bash%0A%20%20%20%20docker%20run%20--runtime%20nvidia%20--gpus%20all%20-p%208001%3A8001%20--ipc%3Dhost%20-e%20VLLM_OPTIONS%3D%22--model%20diffbot%2FLlama-3.1-Diffbot-Small-2412%20--served-model-name%20diffbot-small%20--enable-prefix-caching%22%20%20docker.io%2Fdiffbot%2Fdiffbot-llm-inference%3Alatest%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20For%20the%20larger%2070B%20model%20with%20full%20capabilities%3A%0A%0A%20%20%20%20%60%60%60bash%0A%20%20%20%20docker%20run%20--runtime%20nvidia%20--gpus%20all%20-p%208001%3A8001%20--ipc%3Dhost%20-e%20VLLM_OPTIONS%3D%22--model%20diffbot%2FLlama-3.3-Diffbot-Small-XL-2412%20--served-model-name%20diffbot-small-xl%20--enable-prefix-caching%20--quantization%20fp8%20--tensor-parallel-size%202%22%20%20docker.io%2Fdiffbot%2Fdiffbot-llm-inference%3Alatest%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Once%20the%20application%20starts%20up%20successfully%20and%20you%20see%20a%20message%20like%20the%20following%3A%0A%0A%20%20%20%20%60%60%60plaintext%0A%20%20%20%20INFO%3A%20%20Application%20startup%20complete.%0A%20%20%20%20INFO%3A%20%20Uvicorn%20running%20on%20http%3A%2F%2F0.0.0.0%3A8000%20(Press%20CTRL%2BC%20to%20quit)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20You%20can%20run%20all%20the%20examples%20above%20by%20replacing%20the%20base%20URL%20with%20the%20endpoint%20%60http%3A%2F%2Flocalhost%3A8001%2Frag%2Fv1%60.%0A%0A%20%20%20%20However%2C%20do%20note%20that%20these%20models%20require%20high-end%20GPUs%20like%20A100%20and%20H100s%20to%20run%20at%20full%20precision.%20If%20you%20don't%20have%20the%20right%20hardware%2C%20consider%20using%20%5BRunPod.io%5D(https%3A%2F%2Frunpod.io)%20which%20cost%3A%0A%0A%20%20%20%20-%20%245.98%2Fhr%20for%20dual%20H100%20GPU%20setup%20(total%20160%20GB%20VRAM)%0A%20%20%20%20-%20%241.89%2Fhr%20for%20a%20single%20A100%20GPU%20setup%20(80%20GB%20VRAM)%0A%0A%20%20%20%20!%5B%5D(images%2Frunpod-instances.png)%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%20Building%20real-world%20applications%20with%20Diffbot%20and%20LangChain%0A%0A%20%20%20%20%23%23%23%20LangChain%20%2B%20Diffbot%20basics%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(DIFFBOT_API_TOKEN)%3A%0A%20%20%20%20from%20langchain_openai%20import%20ChatOpenAI%0A%0A%20%20%20%20llm%20%3D%20ChatOpenAI(%0A%20%20%20%20%20%20%20%20model%3D%22diffbot-small-xl%22%2C%0A%20%20%20%20%20%20%20%20temperature%3D0%2C%0A%20%20%20%20%20%20%20%20max_tokens%3DNone%2C%0A%20%20%20%20%20%20%20%20timeout%3DNone%2C%0A%20%20%20%20%20%20%20%20base_url%3D%22https%3A%2F%2Fllm.diffbot.com%2Frag%2Fv1%22%2C%0A%20%20%20%20%20%20%20%20api_key%3DDIFFBOT_API_TOKEN%2C%0A%20%20%20%20)%0A%20%20%20%20return%20ChatOpenAI%2C%20llm%0A%0A%0A%40app.cell%0Adef%20_(llm)%3A%0A%20%20%20%20messages%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22system%22%2C%20%22You%20are%20a%20data%20scientist%20who%20writes%20efficient%20Python%20code.%22)%2C%0A%20%20%20%20%20%20%20%20(%22human%22%2C%20%22Given%20a%20DataFrame%20with%20columns%20'product'%20and%20'sales'%2C%20calculates%20the%20total%20sales%20for%20each%20product.%22)%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20ai_msg%20%3D%20llm.invoke(messages)%0A%20%20%20%20print(ai_msg.content)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(llm)%3A%0A%20%20%20%20from%20langchain_core.prompts%20import%20ChatPromptTemplate%0A%0A%20%20%20%20prompt%20%3D%20ChatPromptTemplate.from_messages(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22system%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22You%20are%20a%20data%20scientist%20who%20writes%20efficient%20%7Blanguage%7D%20code%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22human%22%2C%20%22%7Binput%7D%22)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20chain%20%3D%20prompt%20%7C%20llm%0A%20%20%20%20_result%20%3D%20chain.invoke(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22language%22%3A%20%22SQL%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22input%22%3A%20%22Given%20a%20table%20with%20columns%20'product'%20and%20'sales'%2C%20calculates%20the%20total%20sales%20for%20each%20product.%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20print(_result.content)%0A%20%20%20%20return%20(ChatPromptTemplate%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%23%20Building%20a%20RAG%20application%20with%20Diffbot%20and%20LangChain%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ChatOpenAI%2C%20ChatPromptTemplate)%3A%0A%20%20%20%20import%20json%0A%20%20%20%20from%20typing%20import%20Dict%2C%20List%0A%0A%20%20%20%20from%20langchain_core.output_parsers%20import%20StrOutputParser%0A%0A%20%20%20%20class%20ResearchAssistant%3A%0A%0A%20%20%20%20%20%20%20%20def%20__init__(self%2C%20diffbot_api_key%3A%20str)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.llm%20%3D%20ChatOpenAI(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20model%3D%22diffbot-small-xl%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20temperature%3D0.3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20base_url%3D%22https%3A%2F%2Fllm.diffbot.com%2Frag%2Fv1%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20api_key%3Ddiffbot_api_key%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20self.setup_chains()%0A%0A%20%20%20%20%20%20%20%20def%20setup_chains(self)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.topic_extraction_prompt%20%3D%20ChatPromptTemplate.from_template(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Analyze%20the%20following%20document%20and%20extract%203-5%20main%20topics%20or%20entities%20that%20would%20benefit%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20from%20current%20information.%20Return%20as%20a%20JSON%20list%20of%20topics.%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Document%3A%20%7Bdocument%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Topics%20(JSON%20format)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20self.research_prompt%20%3D%20ChatPromptTemplate.from_template(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Provide%20comprehensive%2C%20current%20information%20about%3A%20%7Btopic%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Context%20from%20document%3A%20%7Bcontext%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Include%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201.%20Current%20status%20and%20recent%20developments%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202.%20Key%20statistics%20or%20data%20points%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%203.%20Recent%20news%20or%20updates%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%204.%20Relevant%20industry%20trends%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Ensure%20all%20facts%20are%20cited%20with%20sources.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20self.report_prompt%20%3D%20ChatPromptTemplate.from_template(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Create%20a%20comprehensive%20research%20report%20based%20on%20the%20document%20analysis%20and%20current%20research.%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Original%20Document%20Summary%3A%20%7Bdocument_summary%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Research%20Findings%3A%20%7Bresearch_findings%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Generate%20a%20well-structured%20report%20that%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201.%20Summarizes%20the%20original%20document's%20main%20points%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202.%20Provides%20current%20context%20for%20each%20major%20topic%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%203.%20Identifies%20any%20outdated%20information%20in%20the%20document%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%204.%20Suggests%20areas%20for%20further%20investigation%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Include%20proper%20citations%20throughout.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20def%20extract_topics(self%2C%20document%3A%20str)%20-%3E%20List%5Bstr%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22Extract%20main%20topics%20from%20the%20document%20for%20research.%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20chain%20%3D%20self.topic_extraction_prompt%20%7C%20self.llm%20%7C%20StrOutputParser()%0A%20%20%20%20%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_result%20%3D%20chain.invoke(%7B%22document%22%3A%20document%7D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20topics%20%3D%20json.loads(_result.strip())%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20topics%20if%20isinstance(topics%2C%20list)%20else%20%5B%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20except%20(json.JSONDecodeError%2C%20Exception)%20as%20e%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(f%22Error%20extracting%20topics%3A%20%7Be%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%5B%5D%0A%0A%20%20%20%20%20%20%20%20def%20research_topic(self%2C%20topic%3A%20str%2C%20context%3A%20str)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22Research%20current%20information%20about%20a%20specific%20topic.%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20chain%20%3D%20self.research_prompt%20%7C%20self.llm%20%7C%20StrOutputParser()%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20chain.invoke(%7B%22topic%22%3A%20topic%2C%20%22context%22%3A%20context%7D)%0A%0A%20%20%20%20%20%20%20%20def%20generate_report(self%2C%20document%3A%20str%2C%20research_findings%3A%20List%5BDict%5D)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22Generate%20comprehensive%20report%20with%20current%20information.%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20summary_prompt%20%3D%20ChatPromptTemplate.from_template(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Provide%20a%20concise%20summary%20of%20this%20document%3A%20%7Bdocument%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20summary_chain%20%3D%20summary_prompt%20%7C%20self.llm%20%7C%20StrOutputParser()%0A%20%20%20%20%20%20%20%20%20%20%20%20document_summary%20%3D%20summary_chain.invoke(%7B%22document%22%3A%20document%7D)%0A%20%20%20%20%20%20%20%20%20%20%20%20findings_text%20%3D%20%22%5Cn%5Cn%22.join(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22**%7Bfinding%5B'topic'%5D%7D%3A**%5Cn%7Bfinding%5B'research'%5D%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20finding%20in%20research_findings%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20report_chain%20%3D%20self.report_prompt%20%7C%20self.llm%20%7C%20StrOutputParser()%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20report_chain.invoke(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22document_summary%22%3A%20document_summary%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22research_findings%22%3A%20findings_text%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20def%20analyze_document(self%2C%20document%3A%20str)%20-%3E%20Dict%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22Complete%20document%20analysis%20with%20current%20research.%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Extracting%20topics%20from%20document...%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20topics%20%3D%20self.extract_topics(document)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20not%20topics%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%7B%22error%22%3A%20%22Could%20not%20extract%20topics%20from%20document%22%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22Researching%20%7Blen(topics)%7D%20topics...%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20research_findings%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20topic%20in%20topics%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%20%20-%20Researching%3A%20%7Btopic%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20research%20%3D%20self.research_topic(topic%2C%20document)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20research_findings.append(%7B%22topic%22%3A%20topic%2C%20%22research%22%3A%20research%7D)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Generating%20comprehensive%20report...%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20final_report%20%3D%20self.generate_report(document%2C%20research_findings)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22topics%22%3A%20topics%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22research_findings%22%3A%20research_findings%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22final_report%22%3A%20final_report%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22status%22%3A%20%22completed%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20return%20(ResearchAssistant%2C)%0A%0A%0A%40app.cell%0Adef%20_(DIFFBOT_API_TOKEN%2C%20ResearchAssistant)%3A%0A%20%20%20%20assistant%20%3D%20ResearchAssistant(DIFFBOT_API_TOKEN)%0A%20%20%20%20sample_document%20%3D%20%22%22%22%0A%20%20%20%20%20%20%20%20Artificial%20Intelligence%20has%20made%20significant%20progress%20in%20natural%20language%20processing.%0A%20%20%20%20%20%20%20%20Companies%20like%20OpenAI%20and%20Google%20have%20released%20powerful%20language%20models.%0A%20%20%20%20%20%20%20%20The%20field%20of%20machine%20learning%20continues%20to%20evolve%20with%20new%20architectures%20and%20techniques.%0A%20%20%20%20%20%20%20%20Investment%20in%20AI%20startups%20reached%20%2425%20billion%20in%202023.%0A%20%20%20%20%22%22%22%0A%20%20%20%20_result%20%3D%20assistant.analyze_document(sample_document)%0A%20%20%20%20print(_result%5B%22final_report%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
+
+
+b5bcdf9cf66566e17a715df758bd512a086afb76539ec77e27c1345acf7fc1b1
+
+