• 카테고리

    질문 & 답변
  • 세부 분야

    데스크톱 앱 개발

  • 해결 여부

    미해결

궁금한 부분이 있습니다.

24.05.01 15:07 작성 조회수 47

0

import openai

api_key = " "

openai.api_key = api_key

def ask_gpt(system, prompt, model="gpt-3.5-turbo"):
    completion = openai.ChatCompletion.create(
    model=model,
    messages=[
        {"role": "system", "content": system},
        {"role": "user", "content": prompt}
    ],
    stream=True
    )
    result = ""

    for chunk in completion:
        delta_data = chunk.choices[0].delta
        if 'role' in delta_data:
            continue
        elif 'content' in delta_data:
            r_text = delta_data['content']
            result += r_text
            print(r_text, end="",flust=True)

ask_gpt(system="you are a helpful assistant." , prompt="사과에 관한 글을 써줘")

 

해당 부분이 작동이 되지 않아서 확인 요청드립니다. api_key 값은 일단 빼두었습니다.

답변 1

답변을 작성해보세요.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.05.03

안녕하세요, 인프런 AI 인턴입니다.

님의 코드를 살펴보았을 때, 작동이 되지 않는 이유 중 하나로 print 함수의 flust 매개변수가 오타로 보입니다. 올바른 매개변수는 flush=True입니다. 해당 부분을 수정 후 다시 시도해보시기 바랍니다.

import openai

api_key = " "

openai.api_key = api_key

def ask_gpt(system, prompt, model="gpt-3.5-turbo"):
    completion = openai.ChatCompletion.create(
    model=model,
    messages=[
        {"role": "system", "content": system},
        {"role": "user", "content": prompt}
    ],
    stream=True
    )
    result = ""

    for chunk in completion:
        delta_data = chunk.choices[0].delta
        if 'role' in delta_data:
            continue
        elif 'content' in delta_data:
            r_text = delta_data['content']
            result += r_text
            print(r_text, end="", flush=True)
            
ask_gpt(system="you are a helpful assistant.", prompt="사과에 관한 글을 써줘")

매개변수 이름과 기능을 정확히 확인하는 것이 중요합니다. 코드 수정 후에도 문제가 지속된다면, 추가적인 오류 메시지나 구체적인 문제 상황을 알려주시면 더 자세한 도움을 드릴 수 있습니다.