## This is a fork/based from https://gist.github.com/wiseman/4a706428eaabf4af1002a07a114f61d6

from io import StringIO
import sys
import os
from typing import Dict, Optional

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents.tools import Tool
from langchain.llms import OpenAI

base_path = os.environ.get('OPENAI_API_BASE', 'http://localhost:8080/v1')
model_name = os.environ.get('MODEL_NAME', 'gpt-3.5-turbo')

class PythonREPL:
    """Simulates a standalone Python REPL."""

    def __init__(self):
        pass        

    def run(self, command: str) -> str:
        """Run command and returns anything printed."""
        old_stdout = sys.stdout
        sys.stdout = mystdout = StringIO()
        try:
            exec(command, globals())
            sys.stdout = old_stdout
            output = mystdout.getvalue()
        except Exception as e:
            sys.stdout = old_stdout
            output = str(e)
        return output

llm = OpenAI(temperature=0.0, openai_api_base=base_path, model_name=model_name)
python_repl = Tool(
        "Python REPL",
        PythonREPL().run,
        """A Python shell. Use this to execute python commands. Input should be a valid python command.
        If you expect output it should be printed out.""",
    )
tools = [python_repl]
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
agent.run("What is the 10th fibonacci number?")
langchain.agents.initialize_agent langchain.llms.OpenAI
[((348, 409), 'os.environ.get', 'os.environ.get', (['"""OPENAI_API_BASE"""', '"""http://localhost:8080/v1"""'], {}), "('OPENAI_API_BASE', 'http://localhost:8080/v1')\n", (362, 409), False, 'import os\n'), ((423, 468), 'os.environ.get', 'os.environ.get', (['"""MODEL_NAME"""', '"""gpt-3.5-turbo"""'], {}), "('MODEL_NAME', 'gpt-3.5-turbo')\n", (437, 468), False, 'import os\n'), ((1003, 1076), 'langchain.llms.OpenAI', 'OpenAI', ([], {'temperature': '(0.0)', 'openai_api_base': 'base_path', 'model_name': 'model_name'}), '(temperature=0.0, openai_api_base=base_path, model_name=model_name)\n', (1009, 1076), False, 'from langchain.llms import OpenAI\n'), ((1345, 1424), 'langchain.agents.initialize_agent', 'initialize_agent', (['tools', 'llm'], {'agent': '"""zero-shot-react-description"""', 'verbose': '(True)'}), "(tools, llm, agent='zero-shot-react-description', verbose=True)\n", (1361, 1424), False, 'from langchain.agents import initialize_agent\n'), ((741, 751), 'io.StringIO', 'StringIO', ([], {}), '()\n', (749, 751), False, 'from io import StringIO\n')]