File size: 1,913 Bytes
236af6f
 
 
 
 
34893f5
236af6f
 
34893f5
fec0bf6
 
 
 
 
 
ddda406
 
fec0bf6
 
 
 
 
236af6f
 
 
fec0bf6
 
 
 
236af6f
 
 
34893f5
236af6f
 
ddda406
236af6f
34893f5
236af6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import gradio as gr
from langchain.document_loaders.base import Document
from langchain.indexes import VectorstoreIndexCreator
from apify_client import ApifyClient
import os

# Update with your OpenAI API key
os.environ["OPENAI_API_KEY"] = "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"

# Page Function to extract website content
page_function_code = """
function pageFunction(context) {
    const $ = context.jQuery;
    const data = {
        title: $('title').text(),
        content: $('body').text(),
        url: context.request.url
    };
    return data;
}
"""

# Function to fetch website content using the updated actor
def fetch_website_content(website_url):
    apify_client = ApifyClient("apify_api_uz0y556N4IG2aLcESj67kmnGSUpHF12XAkLp")
    run_input = {
        "startUrls": [{"url": website_url}],
        "pageFunction": page_function_code
    }
    run = apify_client.actor("moJRLRc85AitArpNN").call(run_input=run_input)
    items = list(apify_client.dataset(run["defaultDatasetId"]).iterate_items())
    return items if items else None

# Fetch and index website content
content = fetch_website_content("https://python.langchain.com/en/latest/")
documents = [Document(page_content=item["content"] or "", metadata={"source": item.get("url", "Unknown URL")}) for item in content]
index = VectorstoreIndexCreator().from_loaders([documents])

# Function for the Gradio UI
def ask_langchain(question):
    result = index.query_with_sources(question)
    answer = result["answer"]
    sources = ", ".join(result["sources"])
    return f"{answer}\n\nSources: {sources}"

# Gradio interface
iface = gr.Interface(fn=ask_langchain, 
                     inputs="text", 
                     outputs="text",
                     live=True,
                     title="LangChain Query",
                     description="Ask a question about LangChain based on the indexed content.")
iface.launch()