File size: 1,617 Bytes
9ca30d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from haystack import Pipeline
from haystack.components.fetchers import LinkContentFetcher
from haystack.components.converters import HTMLToDocument
from haystack.components.builders import PromptBuilder
from haystack.components.generators import OpenAIGenerator
from haystack.utils import Secret

def budget_chatbot(query):
    fetcher = LinkContentFetcher()
    converter = HTMLToDocument()
    prompt_template = """
    According to the contents of this website:
    {% for document in documents %}
      {{document.content}}
    {% endfor %}
    Answer the given question: {{query}}
    Answer:
    """
    prompt_builder = PromptBuilder(template=prompt_template)
    llm = OpenAIGenerator(
        api_key=Secret.from_env_var("MONSTER_API_KEY"),
        api_base_url="https://llm.monsterapi.ai/v1/",
        model="microsoft/Phi-3-mini-4k-instruct",
        generation_kwargs = {"max_tokens": 256}
    )
    pipeline = Pipeline()
    pipeline.add_component("fetcher", fetcher)
    pipeline.add_component("converter", converter)
    pipeline.add_component("prompt", prompt_builder)
    pipeline.add_component("llm", llm)

    pipeline.connect("fetcher.streams", "converter.sources")
    pipeline.connect("converter.documents", "prompt.documents")
    pipeline.connect("prompt.prompt", "llm.prompt")

    result = pipeline.run({"fetcher": {"urls": ["https://example.com/indian-2024-budget"]},
                          "prompt": {"query": query}})
    return result["llm"]["replies"][0]

gr.Interface(fn=budget_chatbot, inputs="text", outputs="text", title="Indian 2024 Budget Chatbot").launch()