Tools: Set up Ollama, NGROK, and LangChain

Tools: Set up Ollama, NGROK, and LangChain

Source: Dev.to

OLLAMA ## LangChain & Ollama ## More Info This post shows how to quickly set up Ollama with ngrok and use it in LangChain. You can easily download for Linux, macOS, and Windows: https://ollama.com/download. Test if it's working: Note: If returns nothing, just execute $ ollama serve. Pull your preferred models: You can find it on the official website for Linux, macOS, and Windows: https://ngrok.com/download Note: Remember to add your authtoken! Now, you can expose Ollama with ngrok and add basic authentication (change the username and password as you desire) This will generate your public URL as in the picture: https://09c6b3946ddd.ngrok-free.app You can test it easily by opening a web browser and pasting the public URL: When you authenticate successfully, it will show “Ollama is running”: You can check the models by accessing the endpoint /api/tags: ✨ The two models pulled before are ready-to-use. First, you need to install langchain and langchain-ollama: Now, you can use in another network your Ollama LLM/Embeddings. For my case, give it the following result: Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse COMMAND_BLOCK: $ curl http://localhost:11434; echo # has to give "Ollama is running" Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: $ curl http://localhost:11434; echo # has to give "Ollama is running" COMMAND_BLOCK: $ curl http://localhost:11434; echo # has to give "Ollama is running" COMMAND_BLOCK: $ ollama pull phi4-mini $ ollama pull nomic-embed-text:v1.5 Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: $ ollama pull phi4-mini $ ollama pull nomic-embed-text:v1.5 COMMAND_BLOCK: $ ollama pull phi4-mini $ ollama pull nomic-embed-text:v1.5 COMMAND_BLOCK: $ ngrok config add-authtoken <your-ngrok-auth-token> Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: $ ngrok config add-authtoken <your-ngrok-auth-token> COMMAND_BLOCK: $ ngrok config add-authtoken <your-ngrok-auth-token> COMMAND_BLOCK: $ ngrok http 11434 --host-header="localhost:11434" --basic-auth="username:password" Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: $ ngrok http 11434 --host-header="localhost:11434" --basic-auth="username:password" COMMAND_BLOCK: $ ngrok http 11434 --host-header="localhost:11434" --basic-auth="username:password" COMMAND_BLOCK: $ pip install langchain langchain-core langchain-ollama Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: $ pip install langchain langchain-core langchain-ollama COMMAND_BLOCK: $ pip install langchain langchain-core langchain-ollama COMMAND_BLOCK: import base64 from langchain_ollama.llms import OllamaLLM from langchain_ollama.embeddings import OllamaEmbeddings BASE_URL = "https://09c6b3946ddd.ngrok-free.app" user_pass = b"username:password" auth_header = base64.b64encode(user_pass).decode("utf-8") client_kwargs = { "headers": { "Authorization": f"Basic {auth_header}" } } llm = OllamaLLM( model="phi4-mini:latest", base_url=BASE_URL, client_kwargs=client_kwargs ) embeddings = OllamaEmbeddings( model="nomic-embed-text:v1.5", base_url=BASE_URL, client_kwargs=client_kwargs ) # testing the llm print(llm.invoke("Hello, Ollama LLM =)")) # testing the embeddings print(embeddings.embed_query("Hello, Ollama Embeddings =)")) Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: import base64 from langchain_ollama.llms import OllamaLLM from langchain_ollama.embeddings import OllamaEmbeddings BASE_URL = "https://09c6b3946ddd.ngrok-free.app" user_pass = b"username:password" auth_header = base64.b64encode(user_pass).decode("utf-8") client_kwargs = { "headers": { "Authorization": f"Basic {auth_header}" } } llm = OllamaLLM( model="phi4-mini:latest", base_url=BASE_URL, client_kwargs=client_kwargs ) embeddings = OllamaEmbeddings( model="nomic-embed-text:v1.5", base_url=BASE_URL, client_kwargs=client_kwargs ) # testing the llm print(llm.invoke("Hello, Ollama LLM =)")) # testing the embeddings print(embeddings.embed_query("Hello, Ollama Embeddings =)")) COMMAND_BLOCK: import base64 from langchain_ollama.llms import OllamaLLM from langchain_ollama.embeddings import OllamaEmbeddings BASE_URL = "https://09c6b3946ddd.ngrok-free.app" user_pass = b"username:password" auth_header = base64.b64encode(user_pass).decode("utf-8") client_kwargs = { "headers": { "Authorization": f"Basic {auth_header}" } } llm = OllamaLLM( model="phi4-mini:latest", base_url=BASE_URL, client_kwargs=client_kwargs ) embeddings = OllamaEmbeddings( model="nomic-embed-text:v1.5", base_url=BASE_URL, client_kwargs=client_kwargs ) # testing the llm print(llm.invoke("Hello, Ollama LLM =)")) # testing the embeddings print(embeddings.embed_query("Hello, Ollama Embeddings =)")) - Expose and Secure Your Self-Hosted Ollama API