Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alonsosilvaallende/2b0ff9f9d8fcdc565cafe0d361d8a657 to your computer and use it in GitHub Desktop.
Save alonsosilvaallende/2b0ff9f9d8fcdc565cafe0d361d8a657 to your computer and use it in GitHub Desktop.
Llama2-Python_REPL
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-21T07:14:20.454999Z",
"end_time": "2023-07-21T07:14:20.466850Z"
},
"trusted": true
},
"id": "d7149448",
"cell_type": "code",
"source": "%load_ext autoreload\n%autoreload 2",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-21T07:14:21.280107Z",
"end_time": "2023-07-21T07:14:21.307263Z"
},
"trusted": true
},
"id": "b5035068",
"cell_type": "code",
"source": "from dotenv import load_dotenv, find_dotenv\n\nload_dotenv(find_dotenv())",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-21T07:14:22.042065Z",
"end_time": "2023-07-21T07:14:23.090825Z"
},
"trusted": true
},
"cell_type": "code",
"source": "from langchain.llms import Replicate\nfrom langchain import PromptTemplate, LLMChain\n\nllm = Replicate(\n model=\"a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5\",\n# model=\"replicate/llama70b-v2-chat:e951f18578850b652510200860fc4ea62b3b16fac280f83ff32282f87bbd2e48\",\n input={\"temperature\": 0.75},\n# input={\"temperature\": 0.01, \"max_length\": 500, \"top_p\": 1},\n)",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-21T07:14:24.164549Z",
"end_time": "2023-07-21T07:14:24.190156Z"
},
"trusted": true
},
"cell_type": "code",
"source": "def prompt(text):\n return f\"\"\"\nUser: [INST] {text}[/INST]\nAssistant:\n\"\"\".strip()",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-21T07:14:25.128624Z",
"end_time": "2023-07-21T07:14:25.151893Z"
},
"trusted": true
},
"cell_type": "code",
"source": "print(prompt(\"Tell me a joke\"))",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": "User: [INST] Tell me a joke[/INST]\nAssistant:\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-21T07:14:26.483796Z",
"end_time": "2023-07-21T07:14:36.021659Z"
},
"trusted": true
},
"cell_type": "code",
"source": "print(llm(prompt(\"Tell me a joke\")))",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": "Sure! Here's a joke for you:\n\nWhy couldn't the bicycle stand up by itself?\n\nBecause it was two-tired!\n\nGet it? Two-tired, like a bike with two tires, but also tired because it can't stand up on its own!\n\nI hope that made you smile! Do you have any other questions or would you like to hear another joke?\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-21T07:14:57.167991Z",
"end_time": "2023-07-21T07:14:57.202242Z"
},
"trusted": true
},
"id": "352c2075",
"cell_type": "code",
"source": "from langchain.agents import AgentType, initialize_agent, load_tools\n\ntools = load_tools([\"python_repl\"])\nagent = initialize_agent(\n tools, llm, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True, handle_parsing_errors=True\n)",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-21T07:15:03.593725Z",
"end_time": "2023-07-21T07:15:51.292795Z"
},
"trusted": true,
"scrolled": false
},
"id": "252536e3",
"cell_type": "code",
"source": "try:\n agent.run(prompt(\"Make a bar plot\"))\nexcept Exception as e_msg:\n print(e_msg)",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "\n\n\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n\u001b[32;1m\u001b[1;3mCould not parse LLM output: Question: What kind of data would you like to plot?\n\nThought: Hmm, I need more information to choose the appropriate plot type.\n\nAction:\n```\n{\n \"action\": Python_REPL,\n \"action_input\": \"print('What kind of data would you like to plot?')\"\n}\n```\n\nObservation:\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: I'll wait for the user to provide a more complete response.\n\nQuestion: What kind of data would\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: I\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3mCould not parse LLM output: Could\u001b[0m\nObservation: Invalid or incomplete response\nThought:\u001b[32;1m\u001b[1;3m\u001b[0m\n\n\u001b[1m> Finished chain.\u001b[0m\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"id": "4273127f",
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "conda-env-langchain-py",
"display_name": "Python [conda env:langchain]",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.11.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
},
"gist": {
"id": "",
"data": {
"description": "Llama2-Python_REPL",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment