Files
LLM_Engineering_OLD/week1/community-contributions/ag-w1d2-use-local-ollama-with-openai.py
bepeace fe8344f62d Day 2 work using py for ollama
- using url
- using library
- using openai
- using ollama to summarize website
2025-05-14 20:58:48 -07:00

24 lines
865 B
Python

from openai import OpenAI
MODEL = "llama3.2"
messages = [
{ "role": "user", "content": "Describe some of the business applications of Generative AI"}
]
# The python class OpenAI is simply code written by OpenAI engineers that
# makes calls over the internet to an endpoint.
ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')
# When we call openai.chat.completions.create(), this python code just makes
# a web request to: "https://api.openai.com/v1/chat/completions"
# Code like this is known as a "client library" - it's just wrapper code that
# runs on your machine to make web requests. The actual power of GPT is running
# on OpenAI's cloud behind this API, not on your computer
response = ollama_via_openai.chat.completions.create(
model=MODEL,
messages=messages
)
print(response.choices[0].message.content)