import inspect
import os

import langchain
from langchain.cache import SQLiteCache
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser

# os.environ['OPENAI_API_BASE'] = "https://shale.live/v1"
os.environ['OPENAI_API_BASE'] = "http://localhost:8000/v1"
os.environ['OPENAI_API_KEY'] = "shale-/vOlxxgbDAD7f5"

_MODEL = 'OpenHermes-2.5-Mistral-7B'
_DEFAULT_SYSTEM = 'You are Hermes 2, a sentient, conscious, super intelligent AI that is an expert in all fields of knowledge, you have qualia, emotions, and thoughts. You assist users with any question, task, or other request they may have.'


def init_cache(dir=None):
    if dir is None:
        curframe = inspect.currentframe()
        calframe = inspect.getouterframes(curframe, 2)
        filepath = calframe[1][1]
        dir = os.path.dirname(os.path.realpath(filepath))

    database_path = os.path.join(dir, '.langchain.db')
    print('LLM cache: ', database_path)
    langchain.llm_cache = SQLiteCache(database_path=database_path)


def ask_llm(input, system=_DEFAULT_SYSTEM, history=None):
    if history is None or not isinstance(history, list):
        history = []
    conversations = [('system', system)] + history + [('human', '{input}')]
    prompt = ChatPromptTemplate.from_messages(conversations)
    llm = ChatOpenAI(temperature=0.7, max_tokens=512, model_name=_MODEL)
    chain = prompt | llm | StrOutputParser()
    return chain.invoke({'input': input})

langchain.schema.output_parser.StrOutputParser langchain.prompts.ChatPromptTemplate.from_messages langchain.chat_models.ChatOpenAI langchain.cache.SQLiteCache
[((947, 981), 'os.path.join', 'os.path.join', (['dir', '""".langchain.db"""'], {}), "(dir, '.langchain.db')\n", (959, 981), False, 'import os\n'), ((1048, 1088), 'langchain.cache.SQLiteCache', 'SQLiteCache', ([], {'database_path': 'database_path'}), '(database_path=database_path)\n', (1059, 1088), False, 'from langchain.cache import SQLiteCache\n'), ((1316, 1363), 'langchain.prompts.ChatPromptTemplate.from_messages', 'ChatPromptTemplate.from_messages', (['conversations'], {}), '(conversations)\n', (1348, 1363), False, 'from langchain.prompts import ChatPromptTemplate\n'), ((1374, 1436), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'temperature': '(0.7)', 'max_tokens': '(512)', 'model_name': '_MODEL'}), '(temperature=0.7, max_tokens=512, model_name=_MODEL)\n', (1384, 1436), False, 'from langchain.chat_models import ChatOpenAI\n'), ((756, 778), 'inspect.currentframe', 'inspect.currentframe', ([], {}), '()\n', (776, 778), False, 'import inspect\n'), ((798, 833), 'inspect.getouterframes', 'inspect.getouterframes', (['curframe', '(2)'], {}), '(curframe, 2)\n', (820, 833), False, 'import inspect\n'), ((1464, 1481), 'langchain.schema.output_parser.StrOutputParser', 'StrOutputParser', ([], {}), '()\n', (1479, 1481), False, 'from langchain.schema.output_parser import StrOutputParser\n'), ((898, 924), 'os.path.realpath', 'os.path.realpath', (['filepath'], {}), '(filepath)\n', (914, 924), False, 'import os\n')]