질의를 하면서 APIKEY 가 필요하다는 메시지가 출력되었다
import requests
import json
url = 'http://localhost:11434/api/generate'
payload = {
'model': 'llama3',
'prompt': '올라마랑 python 예제코드 작성해줘'
}
response = requests.post(url, data=json.dumps(payload))
print(f"Content-Type:{response.headers.get('Content-Type')}")
#
responseStr = ""
import requests
import json
url = 'http://localhost:11434/api/generate'
payload = {
'model': 'llama3',
'prompt': '올라마랑 python 예제코드 작성해줘'
}
response = requests.post(url, data=json.dumps(payload))
print(f"Content-Type:{response.headers.get('Content-Type')}")
#
responseStr = ""
# 응답 상태 확인
if response.status_code == 200:
# NDJSON 데이터 처리
ndjson_data = response.text.strip().split("\n") # 줄바꿈으로 데이터 분리
for line in ndjson_data:
json_obj = json.loads(line) # 각 줄을 JSON 객체로 변환
# print(json_obj)
responseStr += json_obj['response']
else:
print(f"HTTP Error: {response.status_code}")
print(responseStr)
<<console>>
Content-Type:application/x-ndjson
You want me to write an example code for using OpenLama with Python, right? 😊
Here's a simple example of how you can use the OpenLama API in Python:
Firstly, make sure you have installed the `requests` library in your Python environment. If not, you can install it by running `pip install requests`.
Now, let's get started! Here is an example code that sends a request to the OpenLama API to get a list of all available models:
```python
import requests
# Set the API endpoint URL and your own API key (replace 'your_api_key' with your actual API key)
url = "https://api.openlma.com/v1/models"
api_key = "your_api_key"
# Set the headers for the request
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Send a GET request to the API endpoint
response = requests.get(url, headers=headers)
# Check if the response was successful (200 OK)
if response.status_code == 200:
# Get the JSON data from the response
data = response.json()
# Print out the list of available models
for model in data["models"]:
print(f"Model: {model['name']}, Version: {model['version']}")
else:
print("Error:", response.status_code)
```
In this code, we first set the API endpoint URL and our own API key. We then set the headers for the request, including the `Authorization` header with our API key.
Next, we send a GET request to the API endpoint using the `requests.get()` method. If the response is successful (200 OK), we get the JSON data from the response and print out the list of available models.
If the response is not successful (e.g., 404 Not Found or 500 Internal Server Error), we print out an error message with the status code.
That's it! You can modify this code to suit your needs, such as filtering models by specific criteria or downloading model data. Have fun exploring OpenLama with Python! 🎉
2. header 추가
Ollama는 로컬에서 실행되는 LLM(Local Language Model) 서버로, OpenAI와 호환되는 API를 제공합니다. Ollama를 사용할 때 API Key는 보통 OpenAI와 달리 임의의 문자열을 사용하면 됩니다. Ollama는 로컬에서 실행되기 때문에 보안상의 이유로 실제 인증 키가 필요하지 않습니다.
payload = {
'model': 'llama3',
'prompt': '올라마와 python 예제코드 작성해줘'
}
# 헤더에 임의의 API Key 설정
headers = {
"API_KEY": "test-key", # 임의의 문자열을 API Key로 사용
"Content-Type": "application/json"
}
response = requests.post(url, data=json.dumps(payload), headers=headers)
3. 추가 참고
`.env` 파일을 사용하여 API Key를 관리하고 싶다면 다음과 같이 설정할 수 있습니다:
1. 프로젝트 루트 디렉토리에 `.env` 파일 생성:
OLLAMA_API_KEY=test-key
2. Python 코드에서 `dotenv` 라이브러리를 사용하여 환경 변수를 로드:
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("OLLAMA_API_KEY")
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
이 방식은 키를 코드에 직접 하드코딩하지 않고 관리할 수 있도록 도와줍니다.
'미분류' 카테고리의 다른 글
Redis (0) | 2025.01.10 |
---|---|
Failed to calculate the value of task ':compileJava' property 'javaCompiler'. (0) | 2024.12.20 |
ollama generate 예제 (0) | 2024.12.20 |
NotOpenSSLWarning: urllib3 v2.0 only supports OpenSSL 1.1.1+ 에러 (0) | 2024.12.20 |
Ollama 시작하기 (0) | 2024.12.19 |