Jaebi의 Binary는 호남선

Flutter - Open AI 연동 본문

Flutter

Flutter - Open AI 연동

jaebijae 2024. 6. 1. 19:07

Parameters

  • Temperature / Top P: 결과값이 얼마나 랜덤 한지 설정 
    • 자동완성 (Completion)에 용이
    • 0이랑 가까워 질수록 확률이 높은 단어를 선택, 1으로 갈수록 “창의적“이게 됨
    • 예시: My favorite animal is
      • Temperature = 0
My favorite animal is a dog
My favorite animal is a dog
My favorite animal is a dog
      • 10번 넣어도 a dog 라고 나옴, a dog 가 제일 높은 확률




      • Temperature = 1.0
My favorite animal is tiger
My favorite animal is the moose
My favorite animal is a dog because I believe that ...
My favorite animal is a mammal. He replied
My favorite animal is a dragon
      • randomness가 max로 되어 일관성이 없으며 흥미로운 결과가 나옴
  • presence_penalty, frequency_penalty 
    • frequency_penalty: 이미 사용된 단어가 많을수록 다시 선택됨을 방지 (단어 중복 방지)
    • presence_penalty: 텍스트에 존재하는 경우만 고려 (토픽 중복 방지)
    • 답에 중복되는 현상이 발생할시 올리면됨

Test Code

Use

  • 해당 Call을 사용하여 Response 200, 알맞은 Result를 받아옴을 확인
  Future<void> getAnswer() async {
    const String baseUrl = "https://api.openai.com/v1/completions";
    const String apiKey = "sk-**************************************";
    var headers = {
      "Content-Type": "application/json",
      "Authorization": "Bearer $apiKey"
    };

    final response = await http.Client().post(
      Uri.parse(baseUrl),
      body: json.encode({
        "model": "text-davinci-003",
        "prompt": "How much money can i get in 5 years",
        "temperature": 0,
        "top_p": 1,
        "frequency_penalty": 0,
        "presence_penalty": 0,
        "max_tokens": 1000,
      }),
      headers: headers,
    );

    if (response.statusCode == 200) {
      print(await json.decode(response.body));
    } else {
      print(response.reasonPhrase);
    }
  }

 

Request Body

{
    "model": "text-davinci-003",
    "prompt": "my question",
    "temperature": 0,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "max_tokens": 1000
}

 

Response Body

{
    "id": "cmpl-6jhAQgcrt6zwxdqTAYjFVZDWZn7sFF",
    "object": "text_completion",
    "created": 1676348714,
    "model": "text-davinci-003",
    "choices": [
        {
            "text": "\n\nAssuming that the ~~~~~~~~~~~~~",
            "index": 0,
            "logprobs": null,
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 28,
        "completion_tokens": 31,
        "total_tokens": 59
    }
}

 

'Flutter' 카테고리의 다른 글

Flutter - Internet Connection 확인  (0) 2024.06.01
Flutter - Dartz  (0) 2024.06.01
Flutter - 유용한 링크  (0) 2024.06.01
Flutter - 이슈 해결  (0) 2024.06.01
Flutter - Clean Architecture  (0) 2024.06.01