$30 off During Our Annual Pro Sale. View Details »

Go와 GPT _ PaLM2로 슬랙 메시지 감정분류 해보기 _ 한성민 [Go to Busan 2023, Golang Korea]

Go와 GPT _ PaLM2로 슬랙 메시지 감정분류 해보기 _ 한성민 [Go to Busan 2023, Golang Korea]

Sungmin Han

July 19, 2023
Tweet

More Decks by Sungmin Han

Other Decks in Technology

Transcript

  1. Go와 GPT / PaLM2로
    메시지 감정분류 해보기
    한성민 / GDG Golang Korea | ML GDE
    (feat. Go는 어떤 점이 좋을까?)
    Go to Busan 2023

    View Slide

  2. - MLOps Lead at Riiid
    - GDE for ML
    - GDG Golang Korea Organizer
    - Python Mentor at F-Lab
    - Former) Research Engineer at Naver Clova
    - Former) Software Engineer at 심심이
    Speaker
    Golang Korea
    ML GDE

    View Slide

  3. Index
    ● Large Language Model (LLM)
    ● Generative AI
    ● Go Language
    ● In-context Learning
    ● Emotion Classification App
    ● Q&A

    View Slide

  4. Large
    Language
    Model (LLM)

    View Slide

  5. 거대 자연어 모델
    Large Language Model (LLM)

    View Slide

  6. LLM의 기본 동작 방식
    Large Language Model (LLM)

    View Slide

  7. 인간 피드백형 강화학습 (RLHF)
    비지도 학습
    지도 학습 &
    파인튜닝
    인간 피드백형
    강화학습
    Large Language Model (LLM)

    View Slide

  8. Google PaLM2
    Large Language Model (LLM)

    View Slide

  9. Google PaLM2
    Large Language Model (LLM)
    Preview

    View Slide

  10. 미래의 모습이 어쩌면..
    Large Language Model (LLM)
    복잡한 문제

    View Slide

  11. Generative AI

    View Slide

  12. Google I/O 2023
    Generative AI

    View Slide

  13. Image / Vision
    Generative AI
    Midjourney, NovelAI, Stable Diffusion…

    View Slide

  14. Audio / Sound
    Generative AI
    Riffusion MurfAI

    View Slide

  15. NLP / Text
    Generative AI
    Google Bard OpenAI GPT

    View Slide

  16. • 2022년 하반기 부터 수 많은 제품 릴리즈
    • 대기업, 스타트업 불문하고 제품 출시
    • 사람이 보조하고 AI가 주요 작업을 하는..
    Landscape
    Generative AI

    View Slide

  17. Go Language

    View Slide

  18. Go언어의 특징
    Go Language
    - Go routine을 이용한 경량 스레드 사용 가능 #코어가 많은 수록 빠름
    - 학습 진입장벽이 낮음 #적은 문법 키워드 #직관적인 문법 구조
    - 컴파일 속도가 매우매우 빠름!
    - 빠른 런타임 성능

    View Slide

  19. Go언어 키워드
    Go Language
    break default func interface select
    case defer go map struct chan
    else goto package switch const
    fallthrough if range type continue
    for import return var
    1
    2
    3
    4
    5

    View Slide

  20. Goroutine
    Go Language
    https://ssup2.github.io/theory_analysis/Golang_Goroutine_Scheduling/

    View Slide

  21. Goroutine
    Go Language
    goroutine goroutine goroutine
    Go Runtime
    OS Kernel
    User Space
    System Space Syscall

    View Slide

  22. Goroutine Sample
    func f(from string) {
    for i := 0; i < 3; i++ {
    fmt.Println(from, ":", i)
    }
    }
    func main() {
    f("direct")
    go f("goroutine")
    go func(msg string) {
    fmt.Println(msg)
    }("going")
    time.Sleep(time.Second)
    fmt.Println("done")
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    View Slide

  23. Output
    $ go run goroutines.go
    direct : 0
    direct : 1
    direct : 2
    goroutine : 0
    going
    goroutine : 1
    goroutine : 2
    done
    1
    2
    3
    4
    5
    6
    7
    8
    9

    View Slide

  24. Output
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ go run goroutines.go
    direct : 0
    direct : 1
    direct : 2
    goroutine : 0
    going
    goroutine : 1
    goroutine : 2
    done

    View Slide

  25. In-context
    Learning

    View Slide

  26. Shots

    View Slide

  27. 너는 이제부터 감정 분류사의 역할을 하게 될거야.
    입력으로 텍스트가 주어지면, 이를 바탕으로 감정을 예측해주면 돼.
    감정은 “매우 긍정”, “긍정”, “매우 부정”, “부정”, “중립”으로 예측해 줘.
    INPUT:
    Golang Korea는 한국 고 언어 생태계를 가꾸어나가는 커뮤니티 정신이 바탕이 되는 단체입니다.
    OUTPUT:
    긍정
    INPUT:
    Go 언어 느리고 복잡해서 못써먹겠는 언어같아요.
    OUTPUT:
    부정
    감정 분류 Prompt

    View Slide

  28. System Prompt
    너는 이제부터 감정 분류사의 역할을 하게 될거야.
    입력으로 텍스트가 주어지면, 이를 바탕으로 감정을 예측해주면 돼.
    감정은 “매우 긍정”, “긍정”, “매우 부정”, “부정”, “중립”으로 예측해 줘.
    INPUT:
    Golang Korea는 한국 고 언어 생태계를 가꾸어나가는 커뮤니티 정신이 바탕이 되는 단체입니다.
    OUTPUT:
    긍정
    INPUT:
    Go 언어 느리고 복잡해서 못써먹겠는 언어같아요.
    OUTPUT:
    부정

    View Slide

  29. Few shots
    너는 이제부터 감정 분류사의 역할을 하게 될거야.
    입력으로 텍스트가 주어지면, 이를 바탕으로 감정을 예측해주면 돼.
    감정은 “매우 긍정”, “긍정”, “매우 부정”, “부정”, “중립”으로 예측해 줘.
    INPUT:
    Golang Korea는 한국 고 언어 생태계를 가꾸어나가는 커뮤니티 정신이 바탕이 되는 단체입니다.
    OUTPUT:
    긍정
    INPUT:
    Go 언어 느리고 복잡해서 못써먹겠는 언어같아요.
    OUTPUT:
    부정

    View Slide

  30. Test
    INPUT:
    Go to Busan은 부산에서 Go 언어를 알리고 정보를 공
    유하기 위해 만들어진 지역 커뮤니티 활동입니다.
    OUTPUT:

    View Slide

  31. Emotion
    Classification
    App

    View Slide

  32. GPT based Emotion Classification Sample
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    func main() {
    api_key := ""
    instruct := `
    너는 이제부터 감정 분류사의 역할을 하게 될거야.
    입력으로 텍스트가 주어지면, 이를 바탕으로 감정을 예측해주면 돼.
    감정은 “매우 긍정”, “긍정”, “매우 부정”, “부정”, “중립”으로 예측해 줘.
    INPUT:
    Golang Korea는 한국 고 언어 생태계를 가꾸어나가는 커뮤니티 정신이 바탕이 되는 단체입니다.
    OUTPUT:
    긍정
    INPUT:
    Go 언어 느리고 복잡해서 못써먹겠는 언어같아요.
    OUTPUT:
    부정
    `

    View Slide

  33. 18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    c, _ := gpt35.NewClient(api_key)
    fmt.Println("INPUT:")
    in := bufio.NewReader(os.Stdin)
    line, err := in.ReadString('\n')
    fmt.Scanln(&line)
    req := &gpt35.Request{
    Model: gpt35.ModelGpt35Turbo,
    Messages: []*gpt35.Message{
    {
    Role: gpt35.RoleUser,
    Content: instruct,
    },
    {
    Role: gpt35.RoleUser,
    Content: fmt.Sprintf("INPUT:\n%v", line),
    },
    },
    }
    GPT based Emotion Classification Sample

    View Slide

  34. resp, err := c.GetChat(req)
    if err != nil {
    panic(err)
    }
    fmt.Printf("감정 분류 (From INPUT: %v):\n%v", line,
    resp.Choices[0].Message.Content)
    }
    36
    37
    38
    39
    40
    41
    42
    GPT based Emotion Classification Sample

    View Slide

  35. Chatbot Sample

    View Slide

  36. OUTPUT
    Emotion Classification App
    INPUT:
    고랭코리아 너무 좋은 것 같아요!
    감정 분류 (From INPUT: 고랭코리아 너무 좋은 것 같아요!):
    OUTPUT:
    매우 긍정

    View Slide

  37. 너는 이제부터 감정 분류사의 역할을 하게 될거야.
    입력으로 텍스트가 주어지면, 이를 바탕으로 감정을 예측해주면 돼.
    감정은 “매우 긍정”, “긍정”, “매우 부정”, “부정”, “중립”으로 예측해 줘.
    INPUT:
    Golang Korea는 한국 고 언어 생태계를 가꾸어나가는 커뮤니티 정신이 바탕이 되는 단체입니다.
    OUTPUT:
    긍정
    INPUT:
    Go 언어 느리고 복잡해서 못써먹겠는 언어같아요.
    OUTPUT:
    부정
    Prompt

    View Slide

  38. You're about to take on the role of a sentiment classifier.
    Given a piece of text as input, you'll predict a sentiment based on it.
    Predict the sentiment as "Very positive", "Positive", "Very negative", "Negative", or "Neutral".
    INPUT:
    Golang Korea is a community-minded group that cultivates Go Language ecosystem in Korea.
    OUTPUT:
    Positive
    INPUT:
    Go language is slow, complicated, and unusable.
    OUTPUT:
    Negative
    Prompt (English)

    View Slide

  39. OUTPUT
    Emotion Classification App

    View Slide

  40. OUTPUT
    Emotion Classification App

    View Slide

  41. PaLM2 on Golang
    Emotion Classification App

    View Slide

  42. PaLM2 on Golang
    Emotion Classification App
    Could you make a golang to run above http request?
    Don't use aiplatform or VertexAI API, instead of it, Please use
    plain HTTP request by using Go.

    View Slide

  43. PaLM2 based Emotion Classification Sample
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    const (
    apiEndpoint = "us-central1-aiplatform.googleapis.com"
    projectID = "key-utility-388704"
    modelID = "text-bison@001"
    )
    func main() {
    // Create a new HTTP client.
    client := &http.Client{}
    // Create a new request.
    req, err := http.NewRequest("POST",
    fmt.Sprintf("https://%s/v1/projects/%s/locations/us-
    central1/publishers/google/models/%s:predict", apiEndpoint, projectID, modelID),
    nil)
    if err != nil {
    panic(err)
    }
    // Set the authorization header.
    req.Header.Add("Authorization", fmt.Sprintf("Bearer %s",
    os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")))

    View Slide

  44. PaLM2 based Emotion Classification Sample
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    33
    34
    35
    36
    37
    req.Header.Add("Content-Type", "application/json")
    // Set the request body.
    data := []byte(`{
    "instances": [
    {
    "content": "You're about to take on the role of a
    sentiment classifier.
    Given a piece of text as input, you'll predict a sentiment based on it.
    Predict the sentiment as \"Very positive\", \"Positive\", \"Very negative\",
    \"Negative\", or \"Neutral\".
    INPUT:
    Golang Korea is a community-minded group that cultivates Go Language ecosystem in
    Korea.
    OUTPUT:
    Positive
    INPUT:
    Go language is slow, complicated, and unusable.

    View Slide

  45. PaLM2 based Emotion Classification Sample
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    OUTPUT:
    Negative
    INPUT:
    Go to Busan 2023 event seems very bad.. It was the worst session ever in my life.
    OUTPUT:"
    }
    ],
    "parameters": {
    "temperature": 0.2,
    "maxOutputTokens": 256,
    "topP": 0.8,
    "topK": 40
    }
    }`)

    View Slide

  46. PaLM2 based Emotion Classification Sample
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    req.Body = ioutil.NopCloser(bytes.NewReader(data))
    // Send the request.
    resp, err := client.Do(req)
    if err != nil {
    panic(err)
    }
    // Close the response body.
    defer resp.Body.Close()
    // Read the response body.
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
    panic(err)
    }
    // Print the response body.
    fmt.Println(string(body))
    }

    View Slide

  47. {
    "predictions": [
    {
    "safetyAttributes": {
    "scores": [
    0.2,
    0.1,
    0.1
    ],
    "blocked": false,
    "categories": [
    "Finance",
    "Toxic",
    "War & Conflict"
    ]
    },
    "content": "\n Very negative",
    "citationMetadata": {
    "citations": []
    }
    }
    ]
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    OUTPUT

    View Slide

  48. wdep[=
    \=[-0 b
    Q & A

    View Slide