mainly screwing around with ai but since deepseek blocked US users, I decided to make a Collab document that allows you to use the base model with promt chainging. this AI is basically completely free but may not be as good as some of the 32b/70b or the giga 671b param models that they have. use whatever you can without maxing out the GPU, which should be the 14b with my testing (if using the free colab base model). feel free to use any model you want off of Ollama's site located here: https://ollama.com/search
Here's a mockup of all the libraries I used (asked Copt to generate it because I'm lazy)
Used to run shell commands and manage subprocesses.
Example: subprocess.Popen is used to execute shell commands to manage the Ollama server.
Used to run tasks in separate threads to avoid blocking the main execution.
Example: threading.Thread is used to start the Ollama server in a separate thread, allowing the main script to continue running.
Provides various time-related functions.
Example: time.sleep can be used to pause execution for a given number of seconds, though it's not explicitly used in the provided code.
Used to make HTTP requests.
Example: requests.get is used to verify if the Ollama server is running by checking the response from the server.
Provides a way of using operating system-dependent functionality.
Example: os.environ is used to set environment variables for CUDA and NVIDIA libraries, ensuring the system can find and use these libraries.
Used to write concurrent code using the async/await syntax.
Example: asyncio.create_subprocess_exec is used to run subprocesses asynchronously, allowing the script to manage multiple tasks concurrently.
A library for building language model applications.
Example: langchain.prompts.ChatPromptTemplate is used to create prompt templates for the conversation chain.
A specific integration of LangChain for the Ollama language model.
Example: langchain_ollama.ChatOllama is used to initialize the Ollama language model.
Provides a flexible framework for emitting log messages from Python programs.
Example: logging.basicConfig is used to configure the logging system, allowing the script to log information and errors.
Provides tools for creating and managing prompts.
Example: langchain.prompts.HumanMessagePromptTemplate is used to create human message prompts.
Provides tools for creating and managing chains of prompts and responses.
Example: langchain.chains.ConversationChain is used to create a conversation chain.
Provides tools for managing memory in conversations.
Example: langchain.memory.ConversationSummaryBufferMemory is used to manage conversation memory, allowing the AI to remember previous interactions.
Provides tools for managing callbacks.
Example: langchain.callbacks.manager.CallbackManager is used to manage callback handlers.
Provides a callback handler for streaming output to stdout.
Example: langchain.callbacks.streaming_stdout.StreamingStdOutCallbackHandler is used to handle streaming output.
Provides tools for creating and managing agents.
Example: langchain.agents.Tool is used to define tools for the AI agent.
A collection of programs for inspecting and manipulating configuration of PCI devices.
Example: Installed to ensure the system can interact with PCI devices.
A command-line tool for transferring data with URLs.
Example: Used to download and install Ollama.
A community-driven extension of LangChain.
Example: Installed to provide additional functionality for LangChain.
- The script starts by killing any existing Ollama processes and installing necessary packages (
pciutils,curl). - It then installs Ollama using a shell command executed via
curl.
- Environment variables for CUDA and NVIDIA libraries are set to ensure the system can find and use these libraries.
- An asynchronous function
run_processis defined to run shell commands asynchronously. - Another asynchronous function
start_ollama_serveis defined to start the Ollama server. - The server is started in a separate thread using
threading.Threadto avoid blocking the main script.
- The script uses
requests.getto verify if the Ollama server is running by checking the response from the server.
- The script pulls the DeepSeek models using shell commands executed via
ollama pull.
- The script installs necessary Python packages (
langchain,requests,langchain_community,langchain_ollama) usingpip.
- The script initializes the Ollama language model using
ChatOllamafromlangchain_ollama.
- A mock search tool function is defined and added to an agent using
langchain.agents.Toolandinitialize_agent.
- Prompt templates and memory for the conversation are created using
langchain.prompts.ChatPromptTemplateandlangchain.memory.ConversationSummaryBufferMemory.
- A conversation chain is created using
langchain.chains.ConversationChain.
- A chat loop function
chat_with_aiis defined to handle user input and generate responses using the conversation chain. - The chat loop runs until the user exits, handling user input, generating responses, and managing errors.
- The chat loop is run if the script is executed as the main module.
This code is prone to errors as using a jupyter notebook trying to do all of this is not optimal at all. If you have any concerns or questions, feel free to put in an issue or whatnot.
The most common error while running is ollama not seeing the actual model when it's downloaded. I can't tell you why, but just restart the instance and it should work. I'm guessing it's because of Colab's timeout time and connectivity, but if there is something that can be changed or is found, shoot me a msg.