Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to model #20

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
update to model
  • Loading branch information
TuanaCelik committed Jan 17, 2024
commit ffd550bfb4176adb8df1d2311d70d72d9b637b74
111 changes: 56 additions & 55 deletions notebooks/hackernews-custom-component-rag.ipynb
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNp8MxX0RRKKrtRWPnGZ1xM"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "RZ0VPP0Oh9lI"
},
"source": [
"# Building a custom component for RAG pipelines with Haystack 2.0-Beta\n",
"\n",
Expand All @@ -25,10 +13,7 @@
"📚 Check out the [**Customizing RAG Pipelines to Summarize Latest Hacker News Posts with Haystack 2.0 Preview**](https://haystack.deepset.ai/blog/customizing-rag-to-summarize-hacker-news-posts-with-haystack2) article for a detailed run through of this example.\n",
"\n",
"###Install dependencies"
],
"metadata": {
"id": "RZ0VPP0Oh9lI"
}
]
},
{
"cell_type": "code",
Expand All @@ -44,17 +29,22 @@
},
{
"cell_type": "markdown",
"metadata": {
"id": "J6RfNzvOpE7B"
},
"source": [
"## Create a Custom Haystack 2.0 Component\n",
"\n",
"This `HackernewsNewestFetcher` ferches the `last_k` newest posts on Hacker News and returns the contents as a List of Haystack Document objects"
],
"metadata": {
"id": "J6RfNzvOpE7B"
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SLjTO0fv4UaB"
},
"outputs": [],
"source": [
"from typing import List\n",
"from haystack import component, Document\n",
Expand Down Expand Up @@ -83,45 +73,45 @@
" except:\n",
" print(f\"Couldn't download {url}, skipped\")\n",
" return {'articles': docs}\n"
],
"metadata": {
"id": "SLjTO0fv4UaB"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8Z9Q-S55pX-z"
},
"source": [
"## Create a Haystack 2.0 RAG Pipeline\n",
"\n",
"This pipeline uses the components available in the Haystack 2.0 preview package at time of writing (22 September 2023) as well as the custom component we've created above.\n",
"\n",
"The end result is a RAG pipeline designed to provide a list of summaries for each of the `last_k` posts on Hacker News, followes by the source URL."
],
"metadata": {
"id": "8Z9Q-S55pX-z"
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "1nT2-ms3QV0c"
},
"outputs": [],
"source": [
"from getpass import getpass\n",
"\n",
"api_key = getpass(\"OpenAI Key: \")"
],
"metadata": {
"id": "1nT2-ms3QV0c"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "HTxmwMup-5eS"
},
"outputs": [],
"source": [
"from haystack import Pipeline\n",
"from haystack.components.builders.prompt_builder import PromptBuilder\n",
"from haystack.components.generators import GPTGenerator\n",
"from haystack.components.generators import OpenAIGenerator\n",
"\n",
"prompt_template = \"\"\"\n",
"You will be provided a few of the latest posts in HakcerNews, followed by their URL.\n",
Expand All @@ -135,7 +125,7 @@
"\"\"\"\n",
"\n",
"prompt_builder = PromptBuilder(template=prompt_template)\n",
"llm = GPTGenerator(model_name=\"gpt-4\", api_key=api_key)\n",
"llm = OpenAIGenerator(model=\"gpt-4\", api_key=api_key)\n",
"fetcher = HackernewsNewestFetcher()\n",
"\n",
"pipe = Pipeline()\n",
Expand All @@ -145,24 +135,35 @@
"\n",
"pipe.connect(\"hackernews_fetcher.articles\", \"prompt_builder.articles\")\n",
"pipe.connect(\"prompt_builder.prompt\", \"llm.prompt\")"
],
"metadata": {
"id": "HTxmwMup-5eS"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"source": [
"result = pipe.run(data={\"hackernews_fetcher\": {\"last_k\": 3}})\n",
"print(result['llm']['replies'][0])"
],
"execution_count": null,
"metadata": {
"id": "Ex4yeDWA_xMo"
},
"execution_count": null,
"outputs": []
"outputs": [],
"source": [
"result = pipe.run(data={\"hackernews_fetcher\": {\"last_k\": 3}})\n",
"print(result['llm']['replies'][0])"
]
}
]
}
],
"metadata": {
"colab": {
"authorship_tag": "ABX9TyNp8MxX0RRKKrtRWPnGZ1xM",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading