Skip to content

Instantly share code, notes, and snippets.

@koverholt
Created September 18, 2024 02:29
Show Gist options
  • Select an option

  • Save koverholt/eef061253fdbcf8848161d083c236e57 to your computer and use it in GitHub Desktop.

Select an option

Save koverholt/eef061253fdbcf8848161d083c236e57 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "8725db7d-5c55-4eb2-b983-587035b04efe",
"metadata": {},
"outputs": [],
"source": [
"!pip install -q -U requests bs4 lxml google-generativeai tqdm"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "26b825f3-3b50-4ae0-a695-5ab6aae0d0e5",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/koverholt/miniforge3/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import requests\n",
"from bs4 import BeautifulSoup\n",
"import google.generativeai as genai\n",
"import os\n",
"from IPython.display import display, Markdown"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4ec9272d-369b-4c60-a052-1b7d519ceeb5",
"metadata": {},
"outputs": [],
"source": [
"genai.configure(api_key=os.environ[\"API_KEY\"])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f0ad0012-00b7-4b1f-883a-12bbbeab4927",
"metadata": {},
"outputs": [],
"source": [
"def get_arxiv_papers(query: str) -> list:\n",
" \"\"\"Retrieves papers from arXiv matching a query.\n",
"\n",
" Args:\n",
" query: The search query.\n",
"\n",
" Returns:\n",
" A string of XML with the search results\n",
" \"\"\"\n",
" base_url = \"http://export.arxiv.org/api/query?\"\n",
" search_query = f\"search_query=all:{query}&start=0\"\n",
" url = base_url + search_query\n",
" response = requests.get(url)\n",
" soup = BeautifulSoup(response.content, \"xml\") # arXiv API returns XML\n",
" return str(soup)\n",
"\n",
"def get_wikipedia_article(title: str) -> dict:\n",
" \"\"\"Retrieves a Wikipedia article.\n",
"\n",
" Args:\n",
" title: Title of the Wikipedia page.\n",
"\n",
" Returns:\n",
" A dictionary with the title, content, and URL of the article.\n",
" \"\"\"\n",
" url = f\"https://en.wikipedia.org/w/api.php?action=parse&page={title}&prop=text&formatversion=2&format=json\"\n",
" response = requests.get(url)\n",
" data = response.json()\n",
"\n",
" if \"parse\" in data:\n",
" content = data[\"parse\"][\"text\"]\n",
" soup = BeautifulSoup(content, \"html.parser\")\n",
" # Remove unwanted elements (e.g., tables, references) \n",
" for tag in soup.find_all([\"table\", \"sup\"]):\n",
" tag.decompose()\n",
" cleaned_content = soup.get_text()\n",
"\n",
" return {\n",
" \"title\": data[\"parse\"][\"title\"],\n",
" \"content\": cleaned_content,\n",
" \"url\": f\"https://en.wikipedia.org/wiki/{title}\"\n",
" }\n",
" else:\n",
" return {\"error\": \"Article not found\"}\n",
"\n",
"def search_github(query: str, language: str = None, sort: str = \"stars\", order: str = \"desc\", max_results: int = 10) -> list:\n",
" \"\"\"Searches GitHub for repositories based on a query.\n",
"\n",
" Args:\n",
" query: The search query.\n",
" language: Filter by programming language (e.g., \"python\").\n",
" sort: Sort by \"stars\", \"forks\", or \"updated\".\n",
" order: Sort order \"asc\" (ascending) or \"desc\" (descending).\n",
" max_results: Maximum number of results to return.\n",
"\n",
" Returns:\n",
" A list of dictionaries, each containing repository name, description, stars, language, and URL.\n",
" \"\"\"\n",
" base_url = \"https://api.github.com/search/repositories?\"\n",
" search_query = f\"q={query}\"\n",
" if language:\n",
" search_query += f\"+language:{language}\"\n",
" search_query += f\"&sort={sort}&order={order}&per_page={max_results}\"\n",
" url = base_url + search_query\n",
"\n",
" response = requests.get(url)\n",
" data = response.json()\n",
"\n",
" results = []\n",
" if \"items\" in data:\n",
" for item in data[\"items\"]:\n",
" results.append({\n",
" \"name\": item[\"name\"],\n",
" \"description\": item[\"description\"],\n",
" \"stars\": item[\"stargazers_count\"],\n",
" \"language\": item[\"language\"],\n",
" \"url\": item[\"html_url\"]\n",
" })\n",
" return results"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5a93499d-402f-4c87-b3a1-9d61ef64bcdc",
"metadata": {},
"outputs": [],
"source": [
"model = genai.GenerativeModel(model_name='gemini-1.5-pro',\n",
" tools=[\n",
" get_arxiv_papers,\n",
" get_wikipedia_article,\n",
" search_github,\n",
" ])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b96278d5-1020-4f31-9783-9c875c2daa27",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Here are some results:\n",
"\n",
"* **Paper:** The first paper returned by the Arxiv search seems interesting: \"The role of supersymmetry phenomenology in particle physics\" - [http://arxiv.org/abs/hep-ph/0012181v1](http://arxiv.org/abs/hep-ph/0012181v1)\n",
"* **Wikipedia Article:** [https://en.wikipedia.org/wiki/Particle_physics](https://en.wikipedia.org/wiki/Particle_physics)\n",
"* **Code Repo:** This repo seems widely used in particle physics: [https://github.com/scikit-hep/scikit-hep](https://github.com/scikit-hep/scikit-hep)"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"chat = model.start_chat(enable_automatic_function_calling=True)\n",
"response = chat.send_message(\"Give me a link to a cool paper, wiki article, and code repo related to particle physics\")\n",
"display(Markdown(response.text))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "09559a63-2e59-41e0-be4d-8a9fac2cefc7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"response:\n",
"GenerateContentResponse(\n",
" done=True,\n",
" iterator=None,\n",
" result=protos.GenerateContentResponse({\n",
" \"candidates\": [\n",
" {\n",
" \"content\": {\n",
" \"parts\": [\n",
" {\n",
" \"text\": \"Here are some results:\\n\\n* **Paper:** The first paper returned by the Arxiv search seems interesting: \\\"The role of supersymmetry phenomenology in particle physics\\\" - [http://arxiv.org/abs/hep-ph/0012181v1](http://arxiv.org/abs/hep-ph/0012181v1)\\n* **Wikipedia Article:** [https://en.wikipedia.org/wiki/Particle_physics](https://en.wikipedia.org/wiki/Particle_physics)\\n* **Code Repo:** This repo seems widely used in particle physics: [https://github.com/scikit-hep/scikit-hep](https://github.com/scikit-hep/scikit-hep)\"\n",
" }\n",
" ],\n",
" \"role\": \"model\"\n",
" },\n",
" \"finish_reason\": \"STOP\",\n",
" \"index\": 0,\n",
" \"safety_ratings\": [\n",
" {\n",
" \"category\": \"HARM_CATEGORY_SEXUALLY_EXPLICIT\",\n",
" \"probability\": \"NEGLIGIBLE\"\n",
" },\n",
" {\n",
" \"category\": \"HARM_CATEGORY_HATE_SPEECH\",\n",
" \"probability\": \"NEGLIGIBLE\"\n",
" },\n",
" {\n",
" \"category\": \"HARM_CATEGORY_HARASSMENT\",\n",
" \"probability\": \"NEGLIGIBLE\"\n",
" },\n",
" {\n",
" \"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\",\n",
" \"probability\": \"NEGLIGIBLE\"\n",
" }\n",
" ]\n",
" }\n",
" ],\n",
" \"usage_metadata\": {\n",
" \"prompt_token_count\": 14610,\n",
" \"candidates_token_count\": 163,\n",
" \"total_token_count\": 14773\n",
" }\n",
" }),\n",
")"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Full API response\n",
"response"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment