본문 바로가기
상식

chatGPT session 유지하면서 api 호출

by 제타 2023. 3. 20.
반응형

import openai
import requests

# Set up your OpenAI API credentials
openai.api_key = "YOUR_API_KEY"

# Start a session with ChatGPT
session = openai.Session(engine="text-davinci-002")

# Define a prompt to send to ChatGPT
prompt = "What is the meaning of life?"

# Generate a response from ChatGPT
response = session.generate(prompt, temperature=0.5, max_tokens=50)

# Print the response from ChatGPT
print(response.choices[0].text)

# Call an API while maintaining the ChatGPT session
api_url = "https://api.example.com/"
api_response = requests.get(api_url)

# Do something with the API response
print(api_response.json())

# End the ChatGPT session
session.close()

 

 

이 예에서는 먼저 OpenAI API 자격 증명을 설정하고 ChatGPT 엔진으로 세션을 시작합니다. 그런 다음 ChatGPT로 보낼 프롬프트를 정의하고 응답을 생성합니다.

다음으로 라이브러리를 사용하여 API를 호출 requests하고 API 응답으로 작업을 수행합니다. API를 호출하기 전에 ChatGPT 세션을 종료하지 않는다는 점에 유의해야 합니다. 이렇게 하면 세션이 활성 상태로 유지되고 필요에 따라 ChatGPT에서 응답을 계속 생성할 수 있습니다.

마지막으로 ChatGPT 세션을 종료하여 리소스를 정리합니다.

반응형