tavily
Verschillen
Dit geeft de verschillen weer tussen de geselecteerde revisie en de huidige revisie van de pagina.
Beide kanten vorige revisieVorige revisieVolgende revisie | Vorige revisie | ||
tavily [2024/10/01 20:59] โ [Links] a3dijke | tavily [2024/10/13 22:26] (huidige) โ [Tavily doorzoekt opgegeven websites] a3dijke | ||
---|---|---|---|
Regel 30: | Regel 30: | ||
---- | ---- | ||
+ | |||
+ | ===== Voorbeeld gebruik Tavily ===== | ||
+ | Hieronder een Python Class met 4 verschillende manieren om Tavily search te gebruiken in Python. Tavily is niet gratis maar er is wel een gratis account waarbij je 1000 verzoeken per maand kunt doen. Dit zou genoeg moeten zijn om te testen. Je hebt een Tavily Api-key nodig: | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | Installeer Tavily voor Python //(De integratie leeft in het langchain-community pakket. Maar we moeten ook het tavily-python pakket installeren.)// | ||
+ | |||
+ | < | ||
+ | < | ||
+ | |||
+ | ๐ธ Boven iedere functie // | ||
+ | ๐ธ De " | ||
+ | ๐ธ De " | ||
+ | ๐ธ De " | ||
+ | ๐ธ De " | ||
+ | |||
+ | ๐ก **Note:** bij zowel de " | ||
+ | |||
+ | < | ||
+ | from langchain_community.retrievers import TavilySearchAPIRetriever | ||
+ | from langchain_community.tools.tavily_search import TavilySearchResults | ||
+ | from langchain_core.output_parsers import StrOutputParser | ||
+ | from langchain_core.prompts import ChatPromptTemplate | ||
+ | from langchain_core.runnables import RunnablePassthrough | ||
+ | from langchain_openai import ChatOpenAI | ||
+ | import os | ||
+ | |||
+ | |||
+ | # test diverse zoekmethoden | ||
+ | class Test_2: | ||
+ | def __init__(self): | ||
+ | | ||
+ | |||
+ | # | ||
+ | def tavilyKaal(self, | ||
+ | client = TavilyClient() | ||
+ | antw = client.search(user_query) | ||
+ | return antw | ||
+ | |||
+ | # TavilySearchAPIRetriever ======================================= | ||
+ | |||
+ | # https:// | ||
+ | def tavilyLangChainNoChain(self, | ||
+ | retriever = TavilySearchAPIRetriever(k=3) | ||
+ | antw = retriever.invoke(user_query) | ||
+ | |||
+ | return antw | ||
+ | |||
+ | # https:// | ||
+ | def tavilyLangChainWithChain(self, | ||
+ | retriever = TavilySearchAPIRetriever(k=3) | ||
+ | prompt = ChatPromptTemplate.from_template( | ||
+ | """ | ||
+ | |||
+ | Context: {context} | ||
+ | |||
+ | Question: {question}""" | ||
+ | ) | ||
+ | llm = ChatOpenAI(model=" | ||
+ | |||
+ | |||
+ | chain = ( | ||
+ | {" | ||
+ | | prompt | ||
+ | | llm | ||
+ | | StrOutputParser() | ||
+ | ) | ||
+ | antw = chain.invoke(user_query) | ||
+ | return antw | ||
+ | | ||
+ | |||
+ | # ALS TOOL: TavilySearchResults ================================== | ||
+ | |||
+ | # https:// | ||
+ | def tavilyLangChainAlsTool(self, | ||
+ | tool = TavilySearchResults( | ||
+ | max_results=3, | ||
+ | search_depth=" | ||
+ | include_answer=True, | ||
+ | include_raw_content=True, | ||
+ | include_images=True, | ||
+ | # include_domains=[...], | ||
+ | # exclude_domains=[...], | ||
+ | # name=" | ||
+ | # description=" | ||
+ | # args_schema=..., | ||
+ | ) | ||
+ | antw = tool.invoke({" | ||
+ | return antw | ||
+ | | ||
+ | |||
+ | # WERKERS ======================================================== | ||
+ | def format_docs(self, | ||
+ | return " | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== โจ Tavily doorzoekt opgegeven websites ===== | ||
+ | < | ||
+ | from langchain_core.output_parsers import StrOutputParser | ||
+ | from langchain_core.prompts import ChatPromptTemplate | ||
+ | from langchain_core.runnables import RunnablePassthrough | ||
+ | from langchain_openai import ChatOpenAI | ||
+ | import os | ||
+ | import streamlit as st | ||
+ | import requests | ||
+ | |||
+ | |||
+ | class Test_2: | ||
+ | def __init__(self): | ||
+ | os.environ[" | ||
+ | |||
+ | # https:// | ||
+ | def tavilyLangChainWithChain(self, | ||
+ | try: | ||
+ | retriever = TavilySearchAPIRetriever( | ||
+ | k=3, | ||
+ | include_domains=[ | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ], | ||
+ | ) | ||
+ | prompt = ChatPromptTemplate.from_template( | ||
+ | """ | ||
+ | |||
+ | Context: {context} | ||
+ | |||
+ | Question: {question}""" | ||
+ | ) | ||
+ | llm = ChatOpenAI(model=" | ||
+ | |||
+ | chain = ( | ||
+ | {" | ||
+ | | prompt | ||
+ | | llm | ||
+ | | StrOutputParser() | ||
+ | ) | ||
+ | antw = chain.invoke(vraag) | ||
+ | return antw | ||
+ | |||
+ | |||
+ | except requests.exceptions.HTTPError as e: | ||
+ | print(f" | ||
+ | return "Er is een fout opgetreden: Invoer moet minimaal uit twee woorden bestaan!" | ||
+ | | ||
+ | |||
+ | # WERKER ======================================================== | ||
+ | def format_docs(self, | ||
+ | return " | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | |||
tavily.1727809155.txt.gz ยท Laatst gewijzigd: 2024/10/01 20:59 door a3dijke