import gradio as gr from haystack.components.generators import HuggingFaceTGIGenerator generator = HuggingFaceTGIGenerator("mistralai/Mixtral-8x7B-Instruct-v0.1") generator.warm_up() from haystack.components.fetchers.link_content import LinkContentFetcher from haystack.components.converters import HTMLToDocument from haystack.components.preprocessors import DocumentSplitter from haystack.components.rankers import TransformersSimilarityRanker from haystack.components.generators import GPTGenerator from haystack.components.builders.prompt_builder import PromptBuilder from haystack import Pipeline fetcher = LinkContentFetcher() converter = HTMLToDocument() document_splitter = DocumentSplitter(split_by="word", split_length=50) similarity_ranker = TransformersSimilarityRanker(top_k=3) prompt_template = """ According to these documents: {% for doc in documents %} {{ doc.content }} {% endfor %} Answer the given question: {{question}} Answer: """ prompt_builder = PromptBuilder(template=prompt_template) pipeline = Pipeline() pipeline.add_component("fetcher", fetcher) pipeline.add_component("converter", converter) pipeline.add_component("splitter", document_splitter) pipeline.add_component("ranker", similarity_ranker) pipeline.add_component("prompt_builder", prompt_builder) pipeline.add_component("llm", generator) pipeline.connect("fetcher.streams", "converter.sources") pipeline.connect("converter.documents", "splitter.documents") pipeline.connect("splitter.documents", "ranker.documents") pipeline.connect("ranker.documents", "prompt_builder.documents") pipeline.connect("prompt_builder.prompt", "llm.prompt") def respond(prompt, use_rag): if use_rag: result = pipeline.run({"prompt_builder": {"question": prompt}, "ranker": {"query": prompt}, "fetcher": {"urls": ["https://haystack.deepset.ai/blog/introducing-haystack-2-beta-and-advent"]}, "llm":{"generation_kwargs": {"max_new_tokens": 350}}}) return result['llm']['replies'][0] else: result = generator.run(prompt, generation_kwargs={"max_new_tokens": 350}) return result["replies"][0] iface = gr.Interface(fn=respond, inputs=["text", "checkbox"], outputs="text") iface.launch()