isayahc commited on
Commit
37b8ac9
1 Parent(s): 5db25ba

Created using Colaboratory

Browse files
Files changed (1) hide show
  1. Copy_of_local_ollama_agent_.ipynb +315 -0
Copy_of_local_ollama_agent_.ipynb ADDED
@@ -0,0 +1,315 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "authorship_tag": "ABX9TyPaNjrQ4/vWL00DhJIGcmSl",
8
+ "include_colab_link": true
9
+ },
10
+ "kernelspec": {
11
+ "name": "python3",
12
+ "display_name": "Python 3"
13
+ },
14
+ "language_info": {
15
+ "name": "python"
16
+ }
17
+ },
18
+ "cells": [
19
+ {
20
+ "cell_type": "markdown",
21
+ "metadata": {
22
+ "id": "view-in-github",
23
+ "colab_type": "text"
24
+ },
25
+ "source": [
26
+ "<a href=\"https://colab.research.google.com/github/almutareb/InnovationPathfinderAI/blob/agent_notebook/Copy_of_local_ollama_agent_.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "source": [
32
+ "!pip install -U langchain_community -q\n",
33
+ "!pip install -U langchain -q\n",
34
+ "!pip install google-search-results -q\n",
35
+ "!pip install langchainhub -q\n",
36
+ "!pip install text_generation -q\n",
37
+ "!pip install arxiv -q"
38
+ ],
39
+ "metadata": {
40
+ "id": "W125o44IGdna",
41
+ "colab": {
42
+ "base_uri": "https://localhost:8080/"
43
+ },
44
+ "outputId": "c746bf1e-0b97-4eab-c94d-a7f417980d8b"
45
+ },
46
+ "execution_count": 1,
47
+ "outputs": [
48
+ {
49
+ "output_type": "stream",
50
+ "name": "stdout",
51
+ "text": [
52
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.7/1.7 MB\u001b[0m \u001b[31m11.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
53
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m241.2/241.2 kB\u001b[0m \u001b[31m11.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
54
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.5/56.5 kB\u001b[0m \u001b[31m5.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
55
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
56
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m55.4/55.4 kB\u001b[0m \u001b[31m4.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
57
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m815.9/815.9 kB\u001b[0m \u001b[31m7.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
58
+ "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
59
+ " Building wheel for google-search-results (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
60
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m81.1/81.1 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
61
+ "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
62
+ " Building wheel for sgmllib3k (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
63
+ ]
64
+ }
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "code",
69
+ "source": [
70
+ "import os\n",
71
+ "from google.colab import userdata\n",
72
+ "os.environ[\"HUGGINGFACEHUB_API_TOKEN\"] = userdata.get('HUGGINGFACEHUB_API_TOKEN')\n",
73
+ "os.environ[\"SERPAPI_API_KEY\"] = userdata.get('SERPAPI_API_KEY')"
74
+ ],
75
+ "metadata": {
76
+ "id": "yXd9TTjoK439"
77
+ },
78
+ "execution_count": 2,
79
+ "outputs": []
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "execution_count": 7,
84
+ "metadata": {
85
+ "colab": {
86
+ "base_uri": "https://localhost:8080/"
87
+ },
88
+ "id": "sOrq-hGZGZ_r",
89
+ "outputId": "15f1040e-2e50-40dc-f1ef-bdff72097228"
90
+ },
91
+ "outputs": [
92
+ {
93
+ "output_type": "stream",
94
+ "name": "stdout",
95
+ "text": [
96
+ " Why don't astronauts forget things in space?\n",
97
+ "\n",
98
+ "Because in space, they weightless information! 💡🚀\n",
99
+ "\n",
100
+ "(This one is an oldie but goodie!)\n"
101
+ ]
102
+ }
103
+ ],
104
+ "source": [
105
+ "# LangChain supports many other chat models. Here, we're using Ollama\n",
106
+ "from langchain_community.chat_models import ChatOllama\n",
107
+ "from langchain_core.output_parsers import StrOutputParser\n",
108
+ "from langchain_core.prompts import ChatPromptTemplate\n",
109
+ "\n",
110
+ "# supports many more optional parameters. Hover on your `ChatOllama(...)`\n",
111
+ "# class to view the latest available supported parameters\n",
112
+ "llm = ChatOllama(\n",
113
+ " model=\"mistral\",\n",
114
+ " base_url=\"https://0013-35-201-206-176.ngrok-free.app\"\n",
115
+ " )\n",
116
+ "prompt = ChatPromptTemplate.from_template(\"Tell me a short joke about {topic}\")\n",
117
+ "\n",
118
+ "# using LangChain Expressive Language chain syntax\n",
119
+ "# learn more about the LCEL on\n",
120
+ "# https://python.langchain.com/docs/expression_language/why\n",
121
+ "chain = prompt | llm | StrOutputParser()\n",
122
+ "\n",
123
+ "# for brevity, response is printed in terminal\n",
124
+ "# You can use LangServe to deploy your application for\n",
125
+ "# production\n",
126
+ "print(chain.invoke({\"topic\": \"Space travel\"}))"
127
+ ]
128
+ },
129
+ {
130
+ "cell_type": "code",
131
+ "source": [
132
+ "from langchain.tools.retriever import create_retriever_tool\n",
133
+ "from langchain_community.utilities import SerpAPIWrapper\n",
134
+ "from langchain.retrievers import ArxivRetriever\n",
135
+ "from langchain_core.tools import Tool\n",
136
+ "\n",
137
+ "retriever = ArxivRetriever(load_max_docs=2)\n",
138
+ "\n",
139
+ "tools = [create_retriever_tool(\n",
140
+ " retriever,\n",
141
+ " \"search arxiv's database for\",\n",
142
+ " \"Use this to recomend the user a paper to read Unless stated please choose the most recent models\",\n",
143
+ " # \"Searches and returns excerpts from the 2022 State of the Union.\",\n",
144
+ "),\n",
145
+ "\n",
146
+ "Tool(\n",
147
+ " name=\"SerpAPI\",\n",
148
+ " description=\"A low-cost Google Search API. Useful for when you need to answer questions about current events. Input should be a search query.\",\n",
149
+ " func=SerpAPIWrapper().run,\n",
150
+ ")\n",
151
+ "\n",
152
+ "]\n",
153
+ "\n",
154
+ "# tools = [tool]"
155
+ ],
156
+ "metadata": {
157
+ "id": "tn5XMqCVeaQK"
158
+ },
159
+ "execution_count": 8,
160
+ "outputs": []
161
+ },
162
+ {
163
+ "cell_type": "code",
164
+ "source": [
165
+ "from langchain import hub\n",
166
+ "from langchain.agents import AgentExecutor, load_tools\n",
167
+ "from langchain.agents.format_scratchpad import format_log_to_str\n",
168
+ "from langchain.agents.output_parsers import (\n",
169
+ " ReActJsonSingleInputOutputParser,\n",
170
+ ")\n",
171
+ "from langchain.tools.render import render_text_description\n",
172
+ "# from langchain_community.utilities import SerpAPIWrapper\n",
173
+ "# from langchain.retrievers import ArxivRetriever\n",
174
+ "\n",
175
+ "# setup tools\n",
176
+ "# tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n",
177
+ "\n",
178
+ "# tools = load_tools([\"serpapi\", \"llm-math\",create_retriever_tool], llm=llm)\n",
179
+ "\n",
180
+ "# setup ReAct style prompt\n",
181
+ "prompt = hub.pull(\"hwchase17/react-json\")\n",
182
+ "prompt = prompt.partial(\n",
183
+ " tools=render_text_description(tools),\n",
184
+ " tool_names=\", \".join([t.name for t in tools]),\n",
185
+ ")\n",
186
+ "\n",
187
+ "chat_model = llm\n",
188
+ "# define the agent\n",
189
+ "chat_model_with_stop = chat_model.bind(stop=[\"\\nObservation\"])\n",
190
+ "agent = (\n",
191
+ " {\n",
192
+ " \"input\": lambda x: x[\"input\"],\n",
193
+ " \"agent_scratchpad\": lambda x: format_log_to_str(x[\"intermediate_steps\"]),\n",
194
+ " }\n",
195
+ " | prompt\n",
196
+ " | chat_model_with_stop\n",
197
+ " | ReActJsonSingleInputOutputParser()\n",
198
+ ")\n",
199
+ "\n",
200
+ "# instantiate AgentExecutor\n",
201
+ "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True,handle_parsing_errors=True)\n",
202
+ "\n",
203
+ "# agent_executor.invoke(\n",
204
+ "# {\n",
205
+ "# \"input\": \"Who is the current holder of the speed skating world record on 500 meters? What is her current age raised to the 0.43 power?\"\n",
206
+ "# }\n",
207
+ "# )\n",
208
+ "\n",
209
+ "# agent_executor.invoke(\n",
210
+ "# {\n",
211
+ "# \"input\": \"what are large language models and why are they so expensive to run?\"\n",
212
+ "# }\n",
213
+ "# )\n",
214
+ "\n",
215
+ "agent_executor.invoke(\n",
216
+ " {\n",
217
+ " \"input\": \"How to generate videos from images using state of the art macchine learning models\"\n",
218
+ " }\n",
219
+ ")\n",
220
+ "\n",
221
+ "\n"
222
+ ],
223
+ "metadata": {
224
+ "colab": {
225
+ "base_uri": "https://localhost:8080/"
226
+ },
227
+ "id": "VzcIqgcDKZuZ",
228
+ "outputId": "99fe8de1-8539-4fe9-ae24-576aad0daee6"
229
+ },
230
+ "execution_count": 9,
231
+ "outputs": [
232
+ {
233
+ "output_type": "stream",
234
+ "name": "stdout",
235
+ "text": [
236
+ "\n",
237
+ "\n",
238
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
239
+ "\u001b[32;1m\u001b[1;3m Thought: To find the most recent papers on generating videos from images using machine learning models, I will use the \"search arxiv's database for\" tool.\n",
240
+ "\n",
241
+ "Action:\n",
242
+ "```json\n",
243
+ "{\n",
244
+ " \"action\": \"search arxiv's database for\",\n",
245
+ " \"action_input\": \"generating videos from images machine learning\"\n",
246
+ "}\n",
247
+ "```\u001b[0m\u001b[36;1m\u001b[1;3mDiffusion models exhibited tremendous progress in image and video generation,\n",
248
+ "exceeding GANs in quality and diversity. However, they are usually trained on\n",
249
+ "very large datasets and are not naturally adapted to manipulate a given input\n",
250
+ "image or video. In this paper we show how this can be resolved by training a\n",
251
+ "diffusion model on a single input image or video. Our image/video-specific\n",
252
+ "diffusion model (SinFusion) learns the appearance and dynamics of the single\n",
253
+ "image or video, while utilizing the conditioning capabilities of diffusion\n",
254
+ "models. It can solve a wide array of image/video-specific manipulation tasks.\n",
255
+ "In particular, our model can learn from few frames the motion and dynamics of a\n",
256
+ "single input video. It can then generate diverse new video samples of the same\n",
257
+ "dynamic scene, extrapolate short videos into long ones (both forward and\n",
258
+ "backward in time) and perform video upsampling. Most of these tasks are not\n",
259
+ "realizable by current video-specific generation methods.\n",
260
+ "\n",
261
+ "In this work we deal with the problem of high-level event detection in video.\n",
262
+ "Specifically, we study the challenging problems of i) learning to detect video\n",
263
+ "events from solely a textual description of the event, without using any\n",
264
+ "positive video examples, and ii) additionally exploiting very few positive\n",
265
+ "training samples together with a small number of ``related'' videos. For\n",
266
+ "learning only from an event's textual description, we first identify a general\n",
267
+ "learning framework and then study the impact of different design choices for\n",
268
+ "various stages of this framework. For additionally learning from example\n",
269
+ "videos, when true positive training samples are scarce, we employ an extension\n",
270
+ "of the Support Vector Machine that allows us to exploit ``related'' event\n",
271
+ "videos by automatically introducing different weights for subsets of the videos\n",
272
+ "in the overall training set. Experimental evaluations performed on the\n",
273
+ "large-scale TRECVID MED 2014 video dataset provide insight on the effectiveness\n",
274
+ "of the proposed methods.\n",
275
+ "\n",
276
+ "We study the problem of video-to-video synthesis, whose goal is to learn a\n",
277
+ "mapping function from an input source video (e.g., a sequence of semantic\n",
278
+ "segmentation masks) to an output photorealistic video that precisely depicts\n",
279
+ "the content of the source video. While its image counterpart, the\n",
280
+ "image-to-image synthesis problem, is a popular topic, the video-to-video\n",
281
+ "synthesis problem is less explored in the literature. Without understanding\n",
282
+ "temporal dynamics, directly applying existing image synthesis approaches to an\n",
283
+ "input video often results in temporally incoherent videos of low visual\n",
284
+ "quality. In this paper, we propose a novel video-to-video synthesis approach\n",
285
+ "under the generative adversarial learning framework. Through carefully-designed\n",
286
+ "generator and discriminator architectures, coupled with a spatio-temporal\n",
287
+ "adversarial objective, we achieve high-resolution, photorealistic, temporally\n",
288
+ "coherent video results on a diverse set of input formats including segmentation\n",
289
+ "masks, sketches, and poses. Experiments on multiple benchmarks show the\n",
290
+ "advantage of our method compared to strong baselines. In particular, our model\n",
291
+ "is capable of synthesizing 2K resolution videos of street scenes up to 30\n",
292
+ "seconds long, which significantly advances the state-of-the-art of video\n",
293
+ "synthesis. Finally, we apply our approach to future video prediction,\n",
294
+ "outperforming several state-of-the-art competing systems.\u001b[0m\u001b[32;1m\u001b[1;3m This paper discusses several recent studies on generating videos from images using machine learning models. One study introduces SinFusion, a diffusion model that learns the appearance and dynamics of a single image or video for various manipulation tasks. Another study deals with high-level event detection in videos, learning from textual descriptions and exploiting related videos. The third study proposes a novel video-to-video synthesis approach under the generative adversarial learning framework to achieve photorealistic, temporally coherent videos.\n",
295
+ "\n",
296
+ "Final Answer: There are recent studies that discuss generating videos from images using machine learning models. One study introduces SinFusion, a diffusion model for image/video-specific manipulation tasks. Another study deals with high-level event detection in videos using textual descriptions and related videos. The third study proposes a novel video-to-video synthesis approach under the generative adversarial learning framework to achieve photorealistic, temporally coherent videos.\u001b[0m\n",
297
+ "\n",
298
+ "\u001b[1m> Finished chain.\u001b[0m\n"
299
+ ]
300
+ },
301
+ {
302
+ "output_type": "execute_result",
303
+ "data": {
304
+ "text/plain": [
305
+ "{'input': 'How to generate videos from images using state of the art macchine learning models',\n",
306
+ " 'output': 'There are recent studies that discuss generating videos from images using machine learning models. One study introduces SinFusion, a diffusion model for image/video-specific manipulation tasks. Another study deals with high-level event detection in videos using textual descriptions and related videos. The third study proposes a novel video-to-video synthesis approach under the generative adversarial learning framework to achieve photorealistic, temporally coherent videos.'}"
307
+ ]
308
+ },
309
+ "metadata": {},
310
+ "execution_count": 9
311
+ }
312
+ ]
313
+ }
314
+ ]
315
+ }