Skip to content

Instantly share code, notes, and snippets.

@etrobot
Last active November 24, 2023 10:36
Show Gist options
  • Save etrobot/fe026840a1e897a48c1b20793a8e767e to your computer and use it in GitHub Desktop.
Save etrobot/fe026840a1e897a48c1b20793a8e767e to your computer and use it in GitHub Desktop.
Semi-Agent.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/etrobot/fe026840a1e897a48c1b20793a8e767e/semi-agent.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"metadata": {
"id": "aXRILQmCOLfU",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ebbbb5ba-50c0-45a4-ed45-7f3f1b6cfbbe"
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
]
}
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "-AhcvZU6ahEj",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "747de972-bf0d-434d-f3d4-6aff4295683a"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.5/1.5 MB\u001b[0m \u001b[31m20.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.0/75.0 kB\u001b[0m \u001b[31m9.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m220.8/220.8 kB\u001b[0m \u001b[31m24.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m64.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m76.9/76.9 kB\u001b[0m \u001b[31m9.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m57.5/57.5 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.0/3.0 MB\u001b[0m \u001b[31m60.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m6.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
"llmx 0.0.15a0 requires cohere, which is not installed.\u001b[0m\u001b[31m\n",
"\u001b[0m"
]
}
],
"source": [
"!pip install -q google-generativeai duckduckgo-search python-dotenv requests beautifulsoup4 litellm gspread"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "swvHxG9BaqXe",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ba3028c3-9eb0-48b9-8d09-bc82840b32a8"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 3
}
],
"source": [
"from dotenv import load_dotenv\n",
"load_dotenv(dotenv_path='/content/drive/MyDrive/Colab Notebooks/.env')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "BgTpaPOUcZes",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "fd985220-c3b9-4fb8-8466-eb1ce618c08e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Cloning into 'semi-agent'...\n",
"remote: Enumerating objects: 28, done.\u001b[K\n",
"remote: Counting objects: 100% (28/28), done.\u001b[K\n",
"remote: Compressing objects: 100% (20/20), done.\u001b[K\n",
"remote: Total 28 (delta 9), reused 23 (delta 7), pack-reused 0\u001b[K\n",
"Receiving objects: 100% (28/28), 7.85 KiB | 7.85 MiB/s, done.\n",
"Resolving deltas: 100% (9/9), done.\n"
]
}
],
"source": [
"!git clone https://github.com/etrobot/semi-agent"
]
},
{
"cell_type": "code",
"source": [
"# Authenticate and authorize access\n",
"from google.colab import auth\n",
"auth.authenticate_user()"
],
"metadata": {
"id": "9yKLY4by4yRU"
},
"execution_count": 10,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import sys,os,re\n",
"sys.path.append('/content/semi-agent')\n",
"from Tool.browse import search\n",
"from Tool.llm import summarize,makelist\n",
"import pandas as pd\n",
"from google.auth import default\n",
"import gspread\n",
"creds, _ = default()\n",
"gc = gspread.authorize(creds)\n",
"\n",
"# Open the Google Sheets file\n",
"worksheet = gc.open('20231124').sheet1 # Replace 'your_sheet_url' with the URL of your Google Sheets file\n",
"\n",
"# Get all values from the sheet and convert to a DataFrame\n",
"rows = worksheet.get_all_values()\n",
"df = pd.DataFrame.from_records(rows[1:],columns=rows[0])\n",
"df.dropna(inplace=True)\n",
"print(df)\n",
"\n",
"def letterNnum(input_string:str):\n",
" pattern = r\"([a-zA-Z]+)\\s+(\\d+)\"\n",
" matches = re.match(pattern, input_string)\n",
" letters = matches.group(1)\n",
" numbers = matches.group(2)\n",
" return ''.join(letters),int(''.join(numbers))\n",
"\n",
"for k,v in df.iterrows():\n",
" pos= re.findall(r\"\\{\\{(.*?)\\}\\}\",v['Prompt'])\n",
" prompt=v['Prompt']\n",
" print('Step',k,prompt)\n",
"\n",
" for p in pos:\n",
" letter,num=letterNnum(p)\n",
" print('{{%s}}'%p,df[letter].values[num-1])\n",
" prompt=prompt.replace(p,df[letter].values[num-1])\n",
" result=eval(v['Agent'])(prompt)\n",
" df.at[k,'Conclusion']=str(result)\n",
" worksheet.update([df.columns.values.tolist()] + df.values.tolist())\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "eGXN8m17Vmf_",
"outputId": "8e0a0d99-c7d5-47d3-9b9e-6755c7afb55d"
},
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" Prompt Agent Product \\\n",
"0 Collect News about MSFT summarize \n",
"1 Collect News about OpenAI summarize \n",
"2 {{Conclusion 1}},{{Conclusion 2}} summarize \n",
"3 {{Conclusion 3}} makelist \n",
"\n",
" Conclusion Operation \n",
"0 Microsoft (MSFT) announced a new cloud-based b... 🔄 ⏯️ \n",
"1 - OpenAI is an artificial intelligence researc... 🔄 ⏯️ \n",
"2 Microsoft announced a new cloud-based business... 🔄 ⏯️ \n",
"3 ['1. Microsoft announced a new cloud-based bus... 🔄 ⏯️ \n",
"Step 0 Collect News about MSFT\n",
"23\n",
"Step 1 Collect News about OpenAI\n",
"25\n",
"Step 2 {{Conclusion 1}},{{Conclusion 2}} \n",
"{{Conclusion 1}} - Microsoft (MSFT) reported a 19% increase in revenue for the fourth quarter of 2021, beating analyst expectations.\n",
"- The company's cloud computing business, Azure, saw a 50% revenue growth year-over-year.\n",
"- Microsoft's gaming division also performed well, with revenue increasing by 11%.\n",
"- The tech giant announced a $60 billion share buyback program and a 11% increase in its dividend.\n",
"- Microsoft's stock price jumped following the earnings report, reaching a new all-time high.\n",
"{{Conclusion 2}} - OpenAI is an artificial intelligence research lab known for developing advanced AI technologies.\n",
"- The company has gained attention for creating GPT-3, a language model capable of natural language processing.\n",
"- OpenAI has also developed robots, drones, and other advanced AI systems for various applications.\n",
"- The company has faced criticism for the potential risks and ethical implications of its AI developments.\n",
"- OpenAI has also been involved in collaborations with other tech companies and research institutions to further advance AI technology.\n",
"1044\n",
"Step 3 {{Conclusion 3}} \n",
"{{Conclusion 3}} Microsoft reported strong financial performance in the fourth quarter of 2021, with a 19% increase in revenue and significant growth in its cloud computing and gaming divisions. The company also announced a share buyback program and dividend increase, leading to a surge in its stock price. Meanwhile, OpenAI is known for developing advanced AI technologies, including the language model GPT-3, but has faced criticism for the potential risks and ethical implications of its AI developments.\n",
"with suffix: {{Microsoft reported strong financial performance in the fourth quarter of 2021, with a 19% increase in revenue and significant growth in its cloud computing and gaming divisions. The company also announced a share buyback program and dividend increase, leading to a surge in its stock price. Meanwhile, OpenAI is known for developing advanced AI technologies, including the language model GPT-3, but has faced criticism for the potential risks and ethical implications of its AI developments.}} Output keypoints with index\n",
"[\"1. Microsoft's strong financial performance in Q4 2021.\", '2. 19% increase in revenue.', '3. Significant growth in cloud computing and gaming divisions.', '4. Announcement of share buyback program and dividend increase.', '5. Surge in stock price.', '6. OpenAI known for developing advanced AI technologies.', '7. Criticism for potential risks and ethical implications of AI developments.']\n"
]
}
]
}
],
"metadata": {
"colab": {
"provenance": [],
"mount_file_id": "1efxYRVsC_b4a1E2Mqz6qX3cDy9YNnJNL",
"authorship_tag": "ABX9TyMyfsHQODg7sQdCkUhXjOsc",
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment