GPT API를 파이썬에서 사용 시 궁금합니다.

알림
|
X

페이지 정보

작성자 세오 121.♡.44.169
작성일 2024.06.17 20:01
388 조회
0 추천
글쓰기

본문

안녕하세요.


제 웹사이트의 게시글을 Rest API를 이용해 불러온 후 gpt api를 이용해 번역해 게시하는 코드를 작성하려고 합니다.


기존에 claude 용으로 작성한 코드는 문제 없지 잘 작동합니다만.. GPT 용으로 코드를 여러번 변경해봐도 작동이 안됩니다.


```

클로드용 

translated_title = get_text(client.messages.create(
model="claude-3-opus-20240229",
max_tokens=50,
temperature=0.0,
system="You will be provided with a sentence in Korean, and your task is to translate it into Japanese.",
messages=[
{"role": "user", "content": post_title},
{"role": "assistant", "content": ""}
]
).content)


GPT용

response = client.chat.completions.create(

model="gpt-4o",
messages=[
{
"role": "system",
"content": "You will be provided with a sentence in Korean, and your task is to translate it into Japanese."
},
{
"role": "user",
"content": f"{post_title}"
}
],
temperature=0.7,
max_tokens=64,
top_p=1
)
translated_title = response.choices[0].message['content'].strip()

```

클로드의 경우 SYSTEM에 프롬프트를 입력 후 결과물을 바로 받을 수 있었습니다만…


GPT에서는 이런 식으로 활용하는게 안되는 건가요? 


GPT 웹사이트에서 사용하는 것처럼 채팅을 주고 받는 방식으로 활용해야되는건지 아니면 단순히 제가 잘못 사용하고 있는건지 궁금합니다.


감사합니다.





댓글 1 / 1 페이지

아이폰점보님의 댓글

작성자 아이폰점보 (210.♡.239.38)
작성일 06.17 20:31
저는 유료 버전을 쓰고 있지 않아 바로 테스 해볼 수는 없지만 python 라이브러리가 결국은 REST API에 대한 wrapper인 것 같으니 아래 기본 예제를 curl로 호출해서 결과 얻어 오는 것부터 접근해 보시면 어떨까 싶습니다.


curl https://api.openai.com/v1/chat/completions 
  -H "Content-Type: application/json" 
  -H "Authorization: Bearer $OPENAI_API_KEY" 
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Hello!"
      }
    ]
  }'
글쓰기
홈으로 전체메뉴 마이메뉴 새글/새댓글
전체 검색