Create a Linkup account for free to get your API key.
3
Create and use the Linkup Retriever
from langchain_linkup import LinkupSearchRetrieverimport osos.environ["LINKUP_API_KEY"] = "PASTE_YOUR_API_KEY_HERE"retriever = LinkupSearchRetriever(depth="deep", # "fast", "standard" or "deep")# Perform a search querydocuments = retriever.invoke(input="What is Linkup, the new French AI startup?")print(documents)
Parameter
Options
Description
depth
fast, standard, deep
Controls search depth. fast is the fastest with no LLM processing, standard leverages agentic search, deep performs more thorough research.
You need API keys for both Linkup and OpenAI. You can get an OpenAI API key here.
import osfrom dotenv import load_dotenv# Load from .env file if availableload_dotenv()# Or set manuallyos.environ["LINKUP_API_KEY"] = "YOUR_LINKUP_API_KEY"os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
3
Create the RAG pipeline
from typing import Any, Literalfrom langchain_core.documents import Documentfrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.runnables import Runnable, RunnableLambda, RunnablePassthroughfrom langchain_openai import ChatOpenAIfrom langchain_linkup import LinkupSearchRetriever# Configurationquery: str = "What is Linkup, the new French AI startup?"linkup_depth: Literal["fast", "standard", "deep"] = "standard"open_ai_model: str = "gpt-4o-mini"# Initialize retrieverretriever = LinkupSearchRetriever(depth=linkup_depth)# Format documents helper functiondef format_retrieved_documents(docs: list[Document]) -> str: return "\n\n".join( [ f"{document.metadata['name']} ({document.metadata['url']}):\n{document.page_content}" for document in docs ] )# Debug helper functiondef inspect_context(state: dict[str, Any]) -> dict[str, Any]: print(f"Context: {state['context']}\n\n") return state# Create prompt and modelgeneration_prompt_template = """Answer the question based only on the following context:{context}Question: {question}"""prompt = ChatPromptTemplate.from_template(generation_prompt_template)model = ChatOpenAI(model=open_ai_model)
4
Run the pipeline
# Build and execute the chainchain: Runnable[Any, str] = ( {"context": retriever | format_retrieved_documents, "question": RunnablePassthrough()} | RunnableLambda(inspect_context) | prompt | model | StrOutputParser())# Get responseresponse = chain.invoke(input=query)print(f"Response: {response}")
Example Response
Context: Linkup (https://www.linkup.fr):Linkup is a French AI startup that provides a search API for LLMs, enabling them to search the web and access up-to-date information.Response: Linkup is a French AI startup that has developed a search API specifically designed for Large Language Models (LLMs). Their technology allows LLMs to search the web and access current information, which helps overcome the limitation of outdated training data that many AI models face. This enables applications built with LLMs to provide more accurate and up-to-date responses by connecting them to real-time information from the internet.
Need help? Email support@linkup.so, ping us on Discord, or talk to us.