
1.ollama이용해서 curl방식으로 질문하기
ollama를 설치해서 사용하면 기본적으로 아래와 같이 실행후 바로 사용할 수 있습니다.
하지만 이렇게 말고 curl방식으로 새로운 커맨드창에서 질문을 할 수 도 있죠

https://github.com/ollama/ollama/blob/main/docs/api.md
curl http://localhost:11434/api/generate -d '{
"model": "llama3",
"prompt": "Why is the sky blue?",
"stream": false
}'
위에 ollama 가이드에 있는 curl 통신방식인데 위와같이 그대로 복사해서 커맨드 창에 넣으면 아래와 같이 반응이 나온다.

2.Next.js에서 API화 해서 사용하기
하지만 내가 하고싶은거는 API화해서 만들어서 데이터를 fetch해서 웹 화면에서 질문을 주고받고 싶은거기 때문에 이걸
다시 만들어줄 필요가 있다. 2가지 방식으로 다 만들 수 있다. page router, app router에 api를 만들어야하는데 두개는
어차피 폴더 구조상의 차이가 좀 있지 기본 적인 코드는 비슷할거라고 생각한다.
pages/api/generatellama.js
export default async function handler(req, res) {
if (req.method !== 'POST') {
res.setHeader('Allow', ['POST']);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
const { prompt } = req.body;
const response = await fetch('http://localhost:11434/api/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'llama3', //llama3, eeve_gguf
prompt:prompt,// 'Why is the sky blue?',
stream: false
}),
});
const data = await response.json();
res.status(200).json(data);
}
원래 curl 통신으로 주고받던 녀석을 POST요청을 통해 주고받을 수 있도록 코드를 만들어 줬다.
curl http://localhost:11434/api/chat -d '{
"model": "llama3",
"messages": [
{
"role": "user",
"content": "why is the sky blue?"
}
],
"stream": false
}'
위와같이 curl 통신으로 주고받던걸 api화해서 만들면 이제 Next.js 화면단에서도 데이터를 불러와 질문을
주고 받을 수 있다. 물론 이렇게 만들지 않아도 바로 통신도 가능해 보인다.
import axios from 'axios';
const data = {
model: "llama3",
messages: [
{
role: "user",
content: "why is the sky blue?"
}
],
stream: false
};
axios.post('http://localhost:11434/api/chat', data, {
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
console.log('Response:', response.data);
})
.catch(error => {
console.error('Error:', error);
});
다만 Next.js 서버쪽에서 관리를 하는게 좋아보인다.

이러나 저러나 로컬이건 특정서버에서 작동하는 모델을 이와같이 API화해서 만들 수 있다.
어차피 데이터 fetch해서 사용하는것은 프론트의 몫이니까..! 뭔가 점점 모델 사용하는게 쉬워지는 시대로
다가오고 있는것 같다. 점점 회사들마다 사내에서 Private GPT같은거 운영하려는 움직임도 많이보이고 ㅎㅎ..
이런걸 사용자용으로 한번 서비스 출시해보고 싶은데 매번 무산되서 아쉬운 상황이긴하다.