Ck773/llama-cpp
0
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": null,6 "metadata": {},7 "outputs": [],8 "source": [9 "%pip install openai\n",10 "%pip install langchain_openai\n",11 "%pip install langchain\n",12 "%pip install langchain_community"13 ]14 },15 {16 "cell_type": "code",17 "execution_count": null,18 "metadata": {},19 "outputs": [],20 "source": [21 "%pip install --upgrade jupyterlab"22 ]23 },24 {25 "cell_type": "code",26 "execution_count": null,27 "metadata": {},28 "outputs": [],29 "source": [30 "%pip install langchainhub"31 ]32 },33 {34 "cell_type": "markdown",35 "metadata": {},36 "source": [37 "curl --request POST \\\n",38 " --url https://ck773-llama-cpp.hf.space/completion \\\n",39 " --header \"Content-Type: application/json\" \\\n",40 " --data '{\"prompt\": \"Hello\",\"n_predict\": 128}'"41 ]42 },43 {44 "cell_type": "code",45 "execution_count": 28,46 "metadata": {},47 "outputs": [],48 "source": [49 "from langchain_openai import ChatOpenAI\n",50 "from langchain import hub\n",51 "from langchain.agents import create_tool_calling_agent\n",52 "from langchain.agents import AgentExecutor\n",53 "from langchain_community.tools.tavily_search import TavilySearchResults\n",54 "import os\n",55 "\n",56 "os.environ[\"TAVILY_API_KEY\"] = \"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\"\n",57 "# os.environ[\"OPENAI_API_KEY\"] = \"ai\"\n",58 "# os.environ[\"OPENAI_API_BASE\"] = \"https://ck773-llama-cpp.hf.space\"\n",59 "\n",60 "tools = [TavilySearchResults(max_results=3)]\n",61 "\n",62 "llm = ChatOpenAI(model=\"gpt4\", temperature=0)\n",63 "\n"64 ]65 },66 {67 "cell_type": "code",68 "execution_count": 29,69 "metadata": {},70 "outputs": [],71 "source": [72 "# Get the prompt to use - you can modify this!\n",73 "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n",74 "prompt.messages\n",75 "\n",76 "agent = create_tool_calling_agent(llm, tools, prompt)\n",77 "\n",78 "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)"79 ]80 },81 {82 "cell_type": "code",83 "execution_count": 30,84 "metadata": {},85 "outputs": [86 {87 "name": "stdout",88 "output_type": "stream",89 "text": [90 "\n",91 "\n",92 "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",93 "Unexpected exception formatting exception. Falling back to standard exception\n"94 ]95 },96 {97 "name": "stderr",98 "output_type": "stream",99 "text": [100 "Traceback (most recent call last):\n",101 " File \"/home/user/miniconda/lib/python3.9/site-packages/IPython/core/interactiveshell.py\", line 3550, in run_code\n",102 " exec(code_obj, self.user_global_ns, self.user_ns)\n",103 " File \"/tmp/ipykernel_274/3829519960.py\", line 1, in <module>\n",104 " agent_executor.invoke({\"input\": \"hi!\"})\n",105 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain/chains/base.py\", line 163, in invoke\n",106 " run_manager.on_chain_end(outputs)\n",107 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain/chains/base.py\", line 153, in invoke\n",108 " )\n",109 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain/agents/agent.py\", line 1432, in _call\n",110 " {return_value_key: observation},\n",111 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain/agents/agent.py\", line 1138, in _take_next_step\n",112 " yield agent_action\n",113 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain/agents/agent.py\", line 1138, in <listcomp>\n",114 " yield agent_action\n",115 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain/agents/agent.py\", line 1166, in _iter_next_step\n",116 " color=None,\n",117 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain/agents/agent.py\", line 514, in plan\n",118 " \"\"\"\n",119 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain_core/runnables/base.py\", line 2875, in stream\n",120 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain_core/runnables/base.py\", line 2862, in transform\n",121 " async for chunk in self._atransform_stream_with_config(\n",122 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain_core/runnables/base.py\", line 1880, in _transform_stream_with_config\n",123 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain_core/runnables/base.py\", line 2826, in _transform\n",124 " )\n",125 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain_core/runnables/base.py\", line 1283, in transform\n",126 " input,\n",127 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain_core/runnables/base.py\", line 4722, in transform\n",128 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain_core/runnables/base.py\", line 1300, in transform\n",129 " raise\n",130 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py\", line 249, in stream\n",131 " )\n",132 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py\", line 229, in stream\n",133 " name=config.get(\"run_name\"),\n",134 " File \"/home/user/miniconda/lib/python3.9/site-packages/langchain_openai/chat_models/base.py\", line 506, in _stream\n",135 " self,\n",136 " File \"/home/user/miniconda/lib/python3.9/site-packages/openai/_utils/_utils.py\", line 275, in wrapper\n",137 " return func(*args, **kwargs)\n",138 " File \"/home/user/miniconda/lib/python3.9/site-packages/openai/resources/chat/completions.py\", line 581, in create\n",139 " return self._post(\n",140 " File \"/home/user/miniconda/lib/python3.9/site-packages/openai/_base_client.py\", line 1233, in post\n",141 " return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))\n",142 " File \"/home/user/miniconda/lib/python3.9/site-packages/openai/_base_client.py\", line 922, in request\n",143 " return self._request(\n",144 " File \"/home/user/miniconda/lib/python3.9/site-packages/openai/_base_client.py\", line 998, in _request\n",145 " return self._retry_request(\n",146 " File \"/home/user/miniconda/lib/python3.9/site-packages/openai/_base_client.py\", line 1046, in _retry_request\n",147 " return self._request(\n",148 " File \"/home/user/miniconda/lib/python3.9/site-packages/openai/_base_client.py\", line 998, in _request\n",149 " return self._retry_request(\n",150 " File \"/home/user/miniconda/lib/python3.9/site-packages/openai/_base_client.py\", line 1046, in _retry_request\n",151 " return self._request(\n",152 " File \"/home/user/miniconda/lib/python3.9/site-packages/openai/_base_client.py\", line 1013, in _request\n",153 " raise self._make_status_error_from_response(err.response) from None\n",154 "openai.InternalServerError: Error code: 500 - {'error': {'code': 500, 'message': 'Unsupported param: tools', 'type': 'server_error'}}\n",155 "\n",156 "During handling of the above exception, another exception occurred:\n",157 "\n",158 "Traceback (most recent call last):\n",159 " File \"/home/user/miniconda/lib/python3.9/site-packages/IPython/core/interactiveshell.py\", line 2144, in showtraceback\n",160 " stb = self.InteractiveTB.structured_traceback(\n",161 " File \"/home/user/miniconda/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 1435, in structured_traceback\n",162 " return FormattedTB.structured_traceback(\n",163 " File \"/home/user/miniconda/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 1326, in structured_traceback\n",164 " return VerboseTB.structured_traceback(\n",165 " File \"/home/user/miniconda/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 1173, in structured_traceback\n",166 " formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,\n",167 " File \"/home/user/miniconda/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 1088, in format_exception_as_a_whole\n",168 " frames.append(self.format_record(record))\n",169 " File \"/home/user/miniconda/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 970, in format_record\n",170 " frame_info.lines, Colors, self.has_colors, lvals\n",171 " File \"/home/user/miniconda/lib/python3.9/site-packages/IPython/core/ultratb.py\", line 792, in lines\n",172 " return self._sd.lines\n",173 " File \"/home/user/miniconda/lib/python3.9/site-packages/stack_data/utils.py\", line 145, in cached_property_wrapper\n",174 " value = obj.__dict__[self.func.__name__] = self.func(obj)\n",175 " File \"/home/user/miniconda/lib/python3.9/site-packages/stack_data/core.py\", line 734, in lines\n",176 " pieces = self.included_pieces\n",177 " File \"/home/user/miniconda/lib/python3.9/site-packages/stack_data/utils.py\", line 145, in cached_property_wrapper\n",178 " value = obj.__dict__[self.func.__name__] = self.func(obj)\n",179 " File \"/home/user/miniconda/lib/python3.9/site-packages/stack_data/core.py\", line 681, in included_pieces\n",180 " pos = scope_pieces.index(self.executing_piece)\n",181 " File \"/home/user/miniconda/lib/python3.9/site-packages/stack_data/utils.py\", line 145, in cached_property_wrapper\n",182 " value = obj.__dict__[self.func.__name__] = self.func(obj)\n",183 " File \"/home/user/miniconda/lib/python3.9/site-packages/stack_data/core.py\", line 660, in executing_piece\n",184 " return only(\n",185 " File \"/home/user/miniconda/lib/python3.9/site-packages/executing/executing.py\", line 116, in only\n",186 " raise NotOneValueFound('Expected one value, found 0')\n",187 "executing.executing.NotOneValueFound: Expected one value, found 0\n"188 ]189 }190 ],191 "source": [192 "agent_executor.invoke({\"input\": \"hi!\"})\n"193 ]194 },195 {196 "cell_type": "code",197 "execution_count": null,198 "metadata": {},199 "outputs": [],200 "source": [201 "import getpass\n",202 "import os\n",203 "\n",204 "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass()\n",205 "\n",206 "OPENAI_API_KEY\n",207 "OPENAI_API_BASE"208 ]209 },210 {211 "cell_type": "code",212 "execution_count": 6,213 "metadata": {},214 "outputs": [215 {216 "name": "stdout",217 "output_type": "stream",218 "text": [219 "ChatCompletion(id='chatcmpl-Fp5acLsoOFJrUTTevByhKclruDlHKV3B', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=' According to the context provided, the meaning of life, as understood by the devil, is to live a life of sin and debauchery.', role='assistant', function_call=None, tool_calls=None))], created=1713526125, model='mistral-small', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=30, prompt_tokens=71, total_tokens=101))\n"220 ]221 }222 ],223 "source": [224 "from openai import OpenAI\n",225 "\n",226 "# OPENAI_API_KEY=your-mistral-api-key\n",227 "# OPENAI_API_BASE=https://api.mistral.ai/v1\n",228 "# OPENAI_MODEL_NAME=\"mistral-small\"\n",229 "\n",230 "question = \"What is the meaning of life?\"\n",231 "context = \"The according to the devil the meaning of live is to live a life of sin and debauchery.\"\n",232 "\n",233 "client = OpenAI(\n",234 " base_url=\"https://ck773-llama-cpp.hf.space\",\n",235 " api_key=\"tom\",\n",236 ")\n",237 "\n",238 "qa = client.chat.completions.create(\n",239 " model=\"mistral-small\",\n",240 " messages=[\n",241 " {\n",242 " \"role\": \"system\",\n",243 " \"content\": \"You are a system that answers questions based on the context. answer exactly what the question asks using the context.\",\n",244 " },\n",245 " {\n",246 " \"role\": \"user\",\n",247 " \"content\": f\"using the context: {context}\\n\\nAnswer the following question: {question}\",\n",248 " },\n",249 " ],\n",250 ")\n",251 "print(qa)"252 ]253 },254 {255 "cell_type": "code",256 "execution_count": null,257 "metadata": {},258 "outputs": [],259 "source": [260 "from openai import OpenAI\n",261 "from colorama import init\n",262 "from colorama import Fore, Back, Style\n",263 "import time\n",264 "\n",265 "\n",266 "init()\n",267 "\n",268 "client = OpenAI(\n",269 " base_url=\"https://ck773-llama-cpp.hf.space/v1\",\n",270 " api_key=\"tom\",\n",271 ")\n",272 "\n",273 "time.sleep(5)\n",274 "\n",275 "prompts = [\n",276 " \"what is ROI in the context of finance, provide a worked example?\",\n",277 " \"define the efficient frontier in the context of finance\",\n",278 " \"what is glass stegal?\",\n",279 " \"how does derivative pricing work?\",\n",280 "]\n",281 "\n",282 "\n",283 "for prompt in prompts:\n",284 " print(Fore.LIGHTMAGENTA_EX + prompt, end=\"\\n\")\n",285 " response = client.chat.completions.create(\n",286 " model=\"llama.cpp/models/mistral-7b-instruct-v0.1.Q4_0.gguf\",\n",287 " messages=[\n",288 " {\n",289 " \"role\": \"user\",\n",290 " \"content\": prompt,\n",291 " }\n",292 " ],\n",293 " stream=True,\n",294 " max_tokens=1000,\n",295 " )\n",296 " for chunk in response:\n",297 " if chunk.choices[0].delta.content is not None:\n",298 " print(\n",299 " Fore.LIGHTBLUE_EX + chunk.choices[0].delta.content,\n",300 " end=\"\",\n",301 " flush=True,\n",302 " )\n",303 " print(\"\\n\")"304 ]305 },306 {307 "cell_type": "code",308 "execution_count": null,309 "metadata": {},310 "outputs": [],311 "source": [312 "from openai import OpenAI\n",313 "\n",314 "client = OpenAI(base_url=\"https://ck773-llama-cpp.hf.space\", api_key=\"sk-xxx\")\n",315 "response = client.chat.completions.create(\n",316 " model=\"gpt-4-vision-preview\",\n",317 " messages=[\n",318 " {\n",319 " \"role\": \"user\",\n",320 " \"content\": [\n",321 " {\n",322 " \"type\": \"image_url\",\n",323 " \"image_url\": {\n",324 " \"url\": \"https://t4.ftcdn.net/jpg/00/97/58/97/360_F_97589769_t45CqXyzjz0KXwoBZT9PRaWGHRk5hQqQ.jpg\"\n",325 " },\n",326 " },\n",327 " {\"type\": \"text\", \"text\": \"What does the image say\"},\n",328 " ],\n",329 " }\n",330 " ],\n",331 ")\n",332 "print(response)"333 ]334 }335 ],336 "metadata": {337 "kernelspec": {338 "display_name": "Python 3 (ipykernel)",339 "language": "python",340 "name": "python3"341 },342 "language_info": {343 "codemirror_mode": {344 "name": "ipython",345 "version": 3346 },347 "file_extension": ".py",348 "mimetype": "text/x-python",349 "name": "python",350 "nbconvert_exporter": "python",351 "pygments_lexer": "ipython3",352 "version": "3.9.5"353 }354 },355 "nbformat": 4,356 "nbformat_minor": 2357}358 