12  Ollama JSON API Powershell

Ollama serve komutu ile arkaplan hizmeti başlatıyoruz.

ollama serve

Başka bir pencerede veya tab’ta powershell açalım. Aşağıdaki komut ile ollama’nın çalıştığını kontrol edelim.

Invoke-WebRequest http://localhost:11434/

Ollama çağrılarını yaparken json nesneleri kullanıyoruz. En basit çağrımızda model ve süfle (prompt) değerlerini vermemiz gerekir.

{
"model": "ds15", 
"prompt": "What is your name?" 
} 

Aşağıdaki komut ile yukarıdaki json nesnesi ile bir çağrı yapabiliriz.

Invoke-WebRequest -method POST -Body '{"model":"ds15", "prompt":"What is your name?"}' -uri http://localhost:11434/api/generate 

Ama bunun sonucu çok anlamlı olmayacaktır.


StatusCode        : 200
StatusDescription : OK
Content           : {123, 34, 109, 111}
RawContent        : HTTP/1.1 200 OK
                    Date: Fri, 21 Feb 2025 20:03:21 GMT
                    Transfer-Encoding: chunked
                    Content-Type: application/x-ndjson
                    
                    {"model":"ds15","created_at":"2025-02-21T20:03:21.095015583Z","response":"\u003cthink…
Headers           : {[Date, System.String[]], [Transfer-Encoding, System.String[]], [Content-Type, System.String[]]}
RawContentLength  : 4595
RelationLink      : {}

Bizim bu isteğe gelen cevabın içeriğini (Content) almamız gerekiyor. Arkasından bunu json’dan çevirelim. Sonucu akış değil bir defada almak için ayrıca stream false değerinide gönderelim.

(Invoke-WebRequest -method POST -Body '{"model":"ds15", "prompt":"What is your name?", "stream": false}' -uri http://localhost:11434/api/generate ).Content | ConvertFrom-json

Cevabımız aşağıdaki gibi olacaktır. Sizin aldığınız cevap, büyük dil modellerini olasıksal çalışması yüzünden, bundan farklı olabilir.


model                : ds15
created_at           : 2025-02-21T20:06:55.852687211Z
response             : <think>
                       
                       </think>
                       
                       My name is DeepSeek-R1-Lite-Preview. I'm an AI assistant created by DeepSeek. I'll do my best to help you.
done                 : True
done_reason          : stop
context              : {151644, 3838, 374, 697}
total_duration       : 446094159
load_duration        : 15574183
prompt_eval_count    : 8
prompt_eval_duration : 10000000
eval_count           : 36
eval_duration        : 418000000