o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Write efficient MoA python script that uses local LLM like ollama calls.
- User
Write efficient MoA python script that uses local LLM like ollama calls.
- Assistant
Write efficient MoA python script that uses local LLM like ollama calls.
Certainly! Below is an efficient Python script that interfaces with a local Large Language Model (LLM) using Ollama. This script sends prompts to the LLM and retrieves responses, leveraging Ollama's API.
Install Ollama:
ollama run help
Start the Ollama Server:
ollama serve
localhost at port 11434 by default.Python Packages:
requests library installed:
pip install requests
import requests
import json
def query_ollama(prompt, model="llama2"):
url = "http://localhost:11434/api/generate"
headers = {
"Content-Type": "application/json"
}
payload = {
"model": model,
"prompt": prompt
}
try:
response = requests.post(url, headers=headers, json=payload, stream=True)
if response.status_code != 200:
raise Exception(f"Error {response.status_code}: {response.text}")
generated_text = ""
for line in response.iter_lines():
if line:
data = json.loads(line.decode('utf-8'))
generated_text += data.get('response', '')
return generated_text.strip()
except Exception as e:
print(f"An error occurred: {e}")
return None
if __name__ == "__main__":
prompt_text = "Explain the significance of the theory of relativity."
result = query_ollama(prompt_text)
if result:
print("Response from LLM:")
print(result)
Function query_ollama:
prompt: The prompt or question you want to send to the LLM.model: The name of the model you want to use (default is "llama2").stream=True to handle the response in real-time.Main Block:
prompt_text with your query.query_ollama and prints the result if available.Start the Ollama Server:
ollama serve
Leave this terminal window open to keep the server running.
Run the Python Script:
python your_script_name.py
Replace your_script_name.py with the name you saved the script as.
Change the Model:
model parameter when calling query_ollama if you have other models installed.
result = query_ollama(prompt_text, model="your_model_name")
ollama list
Modify the Prompt:
prompt_text variable with your desired question or instruction.Response from LLM:
The theory of relativity, developed by Albert Einstein, revolutionized our understanding of space, time, and gravity. It comprises two interrelated theories: special relativity and general relativity.
1. **Special Relativity (1905):** Introduced the concept that the laws of physics are the same for all non-accelerating observers, and it established the speed of light in a vacuum as the universal speed limit. It led to the famous equation E=mc², showing the equivalence of mass and energy.
2. **General Relativity (1915):** Extended special relativity to include gravity, describing it not as a force but as a curvature of spacetime caused by mass and energy. This theory predicts phenomena such as the bending of light around massive objects, gravitational time dilation, and black holes.
**Significance:**
- **Advancement in Physics:** Challenged Newtonian mechanics and provided a more accurate description of gravitational phenomena.
- **Technological Impact:** Essential for the functionality of GPS satellites, which must account for time dilation effects.
- **Cosmology:** Underpins modern cosmological models, explaining the expansion of the universe and providing a framework for understanding cosmic phenomena.
The theory of relativity has had profound implications across physics, astronomy, and technology, deepening our understanding of the universe.
stream=True allows processing the response incrementally, which is efficient for large outputs.Connection Refused Error:
11434 by default).Model Not Found Error:
ollama pull model_name
Permission Errors:
localhost.Let me know if you need further assistance or have any questions!
Not yet included in a published release.