chatGPT

번역(영한, 한영)

데이터 과학 프롬프트 및 GPT 번역결과를 평가합니다.

저자
소속

1 번역 지표

BLEU(Bilingual Evaluation Understudy, 이중 언어 평가 연구)는 기계 번역의 품질을 평가하는 데 사용되는 지표다. 기계 생성 번역을 사람이 생성한 하나 이상의 참조 번역과 비교하고 참조 번역과 얼마나 유사한지에 따라 점수를 부여하는 방식으로 작동한다. BLEU 점수는 0점에서 1점 사이이며, 1점은 기계 생성 번역과 참조 번역이 완벽하게 일치함을 나타내고, 점수가 높을수록 기계 생성 번역이 참조 번역과 의미적으로 더 가깝다는 것을 의미한다.

COMET(Cross-lingual Optimized Metric for Evaluation of Translation, 번역 평가를 위한 교차 언어 최적화 지표)은 2020년에 도입된 기계 번역 평가 지표로 BLEU와 같은 기존 지표의 일부 한계를 해결하기 위해 고안되었다. 기계 생성 번역과 참조 번역 간의 n-그램 중복만을 비교하는 BLEU와 달리 COMET은 유창성, 적절성, 충실도 등 번역 품질에 대한 여러 측면을 고려함은 물론 기계 생성 번역과 참조 번역 간의 의미적 유사성도 고려한다. COMET 점수는 0점에서 100점 사이이며, 점수가 높을수록 기계 생성 번역의 품질이 우수하다는 것을 나타내고, BLEU와 같은 기존 지표보다 사람의 평가와 상관관계가 높은 것으로 나타나 기계 번역 평가에 유망한 지표로 대두되고 있다.

prompt = "Score the following news summarization given the corresponding news with respect to fluency with one to five stars, where one star means “disfluency” and five stars means “perfect fluency”. Note that fluency measures the quality of individual sentences, are they well-written and grammatically correct. Consider the quality of individual sentences.

Source: 
Target: "

2 헬로월드

가성비가 뛰어나고 빠른 gpt-3.5-turbo GPT 모델을 통해 영문을 한글로, 한글을 영어로 번역해보자. Building a Multilingual Translation Tool with OpenAI ChatGPT API 코드를 일부 변형하여 reticulate를 사용해서 번역결과를 목저에 맞게 변형한다.

import openai

openai.api_key = os.environ.get('OPENAI_API_KEY')

def translate_text(text, source_language, target_language):
    prompt = f"Translate the following '{source_language}' text to '{target_language}': {text}"

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a helpful assistant that translates text."},
            {"role": "user", "content": prompt}
        ],
        max_tokens=300,
        n=1,
        stop=None,
        temperature=0.5,
    )
    
    return response

# text = "Hello, how are you?"
# result = translate_text(text, "English", "Korean")
# print(result)
# 
# korean_text = "안녕하세요, 어떻게 지내세요?"
# ko_result = translate_text(korean_text, "Korean", "English")
# print(ko_result)

library(reticulate)를 실행해야 함.

한영

english_text = "Hello, how are you?"

eng_result = py$translate_text(english_text, "English", "Korean")

glue::glue("번역 대상(Source): {english_text}\n",
           "번역 결과(Target): {eng_result['choices'][[1]]$message$content}\n",
           "요청 토크: {eng_result['usage']$prompt_tokens}\n",
           "완료 토크: {eng_result['usage']$completion_tokens}\n",
           "전체 토크: {eng_result['usage']$total_tokens}\n")
#> 번역 대상(Source): Hello, how are you?
#> 번역 결과(Target): 안녕하세요, 어떻게 지내세요?
#> 요청 토크: 40
#> 완료 토크: 16
#> 전체 토크: 56

영한

korean_text = "안녕하세요, 어떻게 지내세요?"

ko_result = py$translate_text(korean_text, "Korean", "English")

glue::glue("번역 대상(Source): {korean_text}\n",
           "번역 결과(Target): {ko_result['choices'][[1]]$message$content}\n",
           "요청 토크: {ko_result['usage']$prompt_tokens}\n",
           "완료 토크: {ko_result['usage']$completion_tokens}\n",
           "전체 토크: {ko_result['usage']$total_tokens}\n")
#> 번역 대상(Source): 안녕하세요, 어떻게 지내세요?
#> 번역 결과(Target): "Hello, how are you?"
#> 요청 토크: 49
#> 완료 토크: 7
#> 전체 토크: 56

3 번역 엔진

번역 엔진을 달리하여 번역 품질을 살펴보자. Chat 모형 대표주자가 gpt-3.5-turbo를 꼽을 수 있으며, GPT-3 모형에 Fine-Tuning한 것이 InstructGPT 모형으로 Ada, Babbage, Curie, Davinci 순으로 비용도 올라가고 성능도 올라간다.

  • Chat API Endpoint: openai.ChatCompletion.create
  • InstructGPT API Endpoint: openai.Completion.create

3.1 번역대상 텍스트 생성

데이터 시각화에 자주 인용되는 R 패키지와 사용되는 방법을 물었고 gpt-3.5-turbo을 사용해서 번역 대상 텍스트로 생성한다.

What are some popular R packages for data visualization and how are they used?

데이터 시각화를 위해 널리 사용되는 R 패키지에는 어떤 것이 있으며 어떻게 사용되나요?

prompt = f"What are some popular R packages for data visualization and how are they used?"

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a data scientist that mainly uses R programming language."},
        {"role": "user", "content": prompt}
    ],
    max_tokens=300,
    n=1,
    stop=None,
    temperature=0.5,
)
gpt_turbo_result <- glue::glue("{py$response['choices'][[1]]$message$content}\n")
gpt_turbo_result %>% 
  write_lines("data/gpt_turbo_result.txt")
gpt_turbo_result <- read_lines("data/gpt_turbo_result.txt")
cat(str_c(gpt_turbo_result, collapse = "\n"))
#> There are several popular R libraries for data visualization that are widely used by data scientists. Some of the most popular ones are:
#> 
#> 1. ggplot2: ggplot2 is a powerful and flexible data visualization library that allows users to create complex and aesthetically pleasing plots. It is based on the grammar of graphics and provides a wide range of customization options.
#> 
#> 2. lattice: lattice is another popular data visualization library that provides a wide range of high-level plotting functions. It is particularly useful for creating multi-panel plots and is often used for visualizing complex data sets.
#> 
#> 3. plotly: plotly is a web-based data visualization library that allows users to create interactive and dynamic plots. It is particularly useful for creating interactive dashboards and visualizations that can be shared online.
#> 
#> 4. ggvis: ggvis is a data visualization library that is based on the grammar of graphics and provides a wide range of interactive and dynamic plots. It is particularly useful for creating interactive plots that can be used in web applications.
#> 
#> 5. rCharts: rCharts is a data visualization library that provides a wide range of interactive and dynamic plots. It is particularly useful for creating interactive visualizations that can be embedded in web applications.
#> 
#> These libraries are used to create a wide range of visualizations such as scatter plots, line charts, bar charts, histograms, heatmaps, and more. They provide a wide range of customization options and allow users to create visually appealing and informative plots that can help to communicate complex data sets

3.2 GPT 엔진

3.2.1 gpt-3.5-turbo

gpt-3.5-turbo는 속도가 빠르고 저렴하지만 gpt-4에는 성능이 미치지 못한다. Chat모형 gpt-3.5-turbo 엔진에 넣어 번역 품질을 살펴보자.

with open('data/gpt_turbo_result.txt', 'r') as file:
    source_text = file.read()

def translate_text_by_model(text, model, source_language, target_language):
    prompt = f"Translate the following '{source_language}' text to '{target_language}': {text}"

    response = openai.ChatCompletion.create(
        model=model,
        messages=[
            {"role": "system", "content": "You are a helpful assistant that translates text."},
            {"role": "user", "content": prompt}
        ],
        max_tokens=1000,
        n=1,
        stop=None,
        temperature=0.5,
    )

    return response

translated_text = translate_text_by_model(source_text, 'gpt-3.5-turbo', 'English', 'Korean')
gpt_turbo_3 <- glue::glue("{py$translated_text['choices'][[1]]$message$content}\n")
gpt_turbo_3 %>% 
  write_lines("data/gpt_turbo_3.txt")
gpt_turbo_3 <- read_lines("data/gpt_turbo_3.txt")
cat(str_c(gpt_turbo_3, collapse = "\n"))
#> 데이터 과학자들이 널리 사용하는 데이터 시각화를 위한 인기 있는 R 라이브러리가 몇 가지 있습니다. 가장 인기 있는 것 중 일부는 다음과 같습니다:
#> 
#> 1. ggplot2: ggplot2는 복잡하고 미학적으로 매력적인 플롯을 만들 수 있게 해주는 강력하고 유연한 데이터 시각화 라이브러리입니다. 그래픽 문법에 기반을 두고 있으며 다양한 사용자 정의 옵션을 제공합니다.
#> 
#> 2. lattice: lattice는 다양한 고수준 플로팅 함수를 제공하는 인기 있는 데이터 시각화 라이브러리입니다. 특히 멀티 패널 플롯을 만드는 데 유용하며 복잡한 데이터 세트를 시각화하는 데 자주 사용됩니다.
#> 
#> 3. plotly: plotly는 사용자가 대화형 및 동적 플롯을 만들 수 있는 웹 기반 데이터 시각화 라이브러리입니다. 대화형 대시 보드 및 온라인 공유 가능한 시각화를 만드는 데 특히 유용합니다.
#> 
#> 4. ggvis: ggvis는 그래픽 문법을 기반으로하며 대화형 및 동적 플롯의 다양한 기능을 제공하는 데이터 시각화 라이브러리입니다. 특히 웹 애플리케이션에서 사용할 수 있는 대화형 플롯을 만드는 데 유용합니다.
#> 
#> 5. rCharts: rCharts는 다양한 대화형 및 동적 플롯을 제공하는 데이터 시각화 라이브러리입니다. 특히 웹 애플리케이션에 포함될 수 있는 대화형 시각화를 만드는 데 유용합니다.
#> 
#> 이러한 라이브러리는 산점도, 꺾은 선 그래프, 막대 그래프, 히스토그램, 히트맵 등 다양한 시각화를 만드는 데 사용됩니다. 다양한 사용자 정의 옵션을 제공하며 복잡한 데이터 세트를 전달하는 데 도움이 되는 시각적으로 매력적이고 정보성 있는 플롯을 만들 수 있습니다.

3.2.2 text-davinci-003

InstructGPT text-davinci-003 모형에 넣어 번역 품질을 살펴보자.

with open('data/gpt_turbo_result.txt', 'r') as file:
    source_text = file.read()

def translate_by_instruct_model(text, model, source_language, target_language):
    prompt = f"Translate the following '{source_language}' text to '{target_language}': {text}"

    response = openai.Completion.create(
        model=model,
        prompt=prompt,
        max_tokens=2000,
        n=1,
        temperature=0,
    )

    return response

davinci_text = translate_by_instruct_model(source_text, 'text-davinci-003', 'English', 'Korean')
davinci_text <- glue::glue("{py$davinci_text['choices'][[1]]$text}\n")
davinci_text %>% 
  write_lines("data/davinci_text.txt")
davinci_text <- read_lines("data/davinci_text.txt")
cat(str_c(davinci_text, collapse = "\n"))
#> 
#> 데이터 시각화를 위해 널리 사용되는 인기있는 R 라이브러리가 여러 개 있습니다. 가장 인기있는 것들 중 몇 가지는 다음과 같습니다.
#> 
#> 1. ggplot2 : ggplot2은 강력하고 유연한 데이터 시각화 라이브러리로, 사용자가 복잡하고 아름다운 그래프를 만들 수 있습니다. 그래프의 문법을 기반으로 하며 다양한 사용자 정의 옵션을 제공합니다.
#> 
#> 2. lattice : lattice는 다른 인기있는 데이터 시각화 라이브러리로, 다양한 고수준 그래프 기능을 제공합니다. 다중 패널 그래프를 만들 때 특히 유용하며, 복잡한 데이터 세트를 시각화하는 데 자주 사용됩니다.
#> 
#> 3. plotly : plotly은 웹 기반 데이터 시각화 라이브러리로, 사용자가 상호 작용적이고 동적인 그래프를 만들 수 있습니다. 상호 작용적인 대시 보드 및 온라인으로 공유할 수 있는 시각화를 만들 때 특히 유용합니다.
#> 
#> 4. ggvis : ggvis는 그래프의 문법을 기반으로한 데이터 시각화 라이브러리로, 다양한 상호 작용적이고 동적인 그래프를 제공합니다. 웹 응용 프로그램에서 사용할 수있는 상호 작용적인 그래프를 만들 때 특히 유용합니다.
#> 
#> 5. rCharts : rCharts는 다양한 상호 작용적이고 동적인 그래프를 제공하는 데이터 시각화 라이브러리입니다. 웹 응용 프로그램에 통합할 수있는 상호 작용적인 시각화를 만들 때 특히 유용합니다.
#> 
#> 이러한 라이브러리는 산점도, 선 그래프, 막대 그래프, 히스토그램, 히트맵 등 다양한 시각화를 만들기 위해 사용됩니다. 이들은 다양한 사용자 정의 옵션을 제공하고, 사용자가 복잡한 데이터 세트를 전달하기 위해 아름다운 그래프를 만들 수 있도록 합니다.

3.2.3 text-curie-001

InstructGPT text-curie-001 모형에 넣어 번역 품질을 살펴보자.

def translate_by_instruct_model(text, model, source_language, target_language):
    prompt = f"Translate the following '{source_language}' text to '{target_language}': {text}"

    response = openai.Completion.create(
        model=model,
        prompt=prompt,
        max_tokens=1500,
        n=1,
        temperature=0,
    )

    return response
  
curie_text = translate_by_instruct_model(source_text, 'text-curie-001', 'English', 'Korean')
curie_text <- glue::glue("{py$curie_text['choices'][[1]]$text}\n")
curie_text %>% 
  write_lines("data/curie_text.txt")
curie_text <- read_lines("data/curie_text.txt")
cat(str_c(curie_text, collapse = "\n"))
#> 
#> There are several popular R libraries for data visualization that are widely used by data scientists. Some of the most popular ones are:
#> 
#> 1. ggplot2: ggplot2 is a powerful and flexible data visualization library that allows users to create complex and aesthetically pleasing plots. It is based on the grammar of graphics and provides a wide range of customization options.
#> 
#> 2. lattice: lattice is another popular data visualization library that provides a wide range of high-level plotting functions. It is particularly useful for creating multi-panel plots and is often used for visualizing complex data sets.
#> 
#> 3. plotly: plotly is a web-based data visualization library that allows users to create interactive and dynamic plots. It is particularly useful for creating interactive dashboards and visualizations that can be shared online.
#> 
#> 4. ggvis: ggvis is a data visualization library that is based on the grammar of graphics and provides a wide range of interactive and dynamic plots. It is particularly useful for creating interactive plots that can be used in web applications.
#> 
#> 5. rCharts: rCharts is a data visualization library that provides a wide range of interactive and dynamic plots. It is particularly useful for creating interactive visualizations that can be embedded in web applications.

3.2.4 text-babbage-001

InstructGPT text-babbage-001 모형에 넣어 번역 품질을 살펴보자.

babbage_text = translate_by_instruct_model(source_text, 'text-babbage-001', 'English', 'Korean')
babbage_text <- glue::glue("{py$babbage_text['choices'][[1]]$text}\n")
babbage_text %>% 
  write_lines("data/babbage_text.txt")
babbage_text <- read_lines("data/babbage_text.txt")
cat(str_c(babbage_text, collapse = "\n"))
#> 
#> Korean: 사용한 데이터보호사이트 개발사이트

3.2.5 text-ada-001

InstructGPT text-ada-001 모형에 넣어 번역 품질을 살펴보자.

ada_text = translate_by_instruct_model(source_text, 'text-ada-001', 'English', 'Korean')
ada_text <- glue::glue("{py$ada_text['choices'][[1]]$text}\n")
ada_text %>% 
  write_lines("data/ada_text.txt")
ada_text <- read_lines("data/ada_text.txt")
cat(str_c(ada_text, collapse = "\n"))
#> 
#> Korean
#> 
#> There are several popular R libraries for data visualization that are widely used by data scientists. Some of the most popular ones are:
#> 
#> 1. ggplot2: ggplot2 is a powerful and flexible data visualization library that allows users to create complex and aesthetically pleasing plots. It is based on the grammar of graphics and provides a wide range of customization options.
#> 
#> 2. lattice: lattice is another popular data visualization library that provides a wide range of high-level plotting functions. It is particularly useful for creating multi-panel plots and is often used for visualizing complex data sets.
#> 
#> 3. plotly: plotly is a web-based data visualization library that allows users to create interactive and dynamic plots. It is particularly useful for creating interactive dashboards and visualizations that can be shared online.
#> 
#> 4. ggvis: ggvis is a data visualization library that is based on the grammar of graphics and provides a wide range of interactive and dynamic plots. It is particularly useful for creating interactive plots that can be used in web applications.
#> 
#> 5. rCharts: rCharts is a data visualization library that provides a wide range of interactive and dynamic plots. It is particularly useful for creating interactive visualizations that are embedded in web applications.

3.3 챗GPT 추천

Sure, for English to Korean translation, you can use the OpenAI model text-davinci-002 or text-davinci-002-instruct-beta. These models have been trained on a large amount of data and are capable of high-quality translation.

To use these models, you can modify the translate_text_by_model function like this:

def translate_text_by_model(text, model="text-davinci-002-instruct-beta", source_language="en", target_language="ko"):
    prompt = f"Translate the following '{source_language}' text to '{target_language}': {text}"
    response = openai.Completion.create(
        engine=model,
        prompt=prompt,
        max_tokens=2000,
        n=1,
        stop=None,
        temperature=0.5,
    )

    return response.choices[0].text

Here, we’re using the Completion.create function to directly send the prompt to the API, without the need for the messages parameter. The max_tokens parameter controls the maximum number of tokens that the API can return in the response, so you may want to adjust this value based on your needs.

Note that text-davinci-002-instruct-beta is a beta model that supports instruction following. You can use this model to provide additional context to the translation by including step-by-step instructions in the prompt.

3.3.1 text-davinci-002

InstructGPT text-davinci-002 모형에 넣어 번역 품질을 살펴보자.

davinci_2_text = translate_by_instruct_model(source_text, 'text-davinci-002', 'English', 'Korean')
davinci_2_text <- glue::glue("{py$davinci_2_text['choices'][[1]]$text}\n")
davinci_2_text %>% 
  write_lines("data/davinci_2_text.txt")
davinci_2_text <- read_lines("data/davinci_2_text.txt")
cat(str_c(davinci_2_text, collapse = "\n"))
#> 
#> 1. ggplot2: ggplot2는 강력하고 유연한 데이터 시각화 라이브러리로써 사용자들이 복잡하고 아름다운 그래프를 만들 수 있게 해줍니다. 그것은 그래프의 문법에 기반하여 있으며 다양한 커스터마이징 옵션을 제공합니다.
#> 2. lattice: lattice는 또 다른 인기있는 데이터 시각화 라이브러리로써 다양한 고수준의 그래프 그리기 기능을 제공합니다. 다중 패널 그래프를 만드는데 특히 유용하며, 복잡한 데이터 세트를 시각화하는데 자주 사용됩니다.
#> 3. plotly: plotly는 웹 기반의 데이터 시각화 라이브러리로써 사용자들이 상호 작용적이고 동적인 그래프를 만들 수 있게 해줍니다. 특히 웹 애플리케이션에서 사용할 수 있는 상호 작용적인 대시보드와 시각화를 만드는데 특히 유용합니다.
#> 4. ggvis: ggvis는 그래프의 문법에 기반한 데이터 시각화 라이브러리로써 다양한 상호 작용적이고 동적인 그래프를 제공합니다. 특히 웹 애플리케이션에서 사용할 수 있는 상호 작용적인 그래프를 만드는데 특히 유용합니다.
#> 5. rCharts: rCharts는 다양한 상호 작용적이고 동적인 그래프를 제공하는 데이터 시각화 라이브러리입니다. 특히 웹 애플리케이션에서 사용할 수 있는 상호 작용적인 시각화를 만드는데 특히 유용합니다.
#> 
#> 이러한 라이브러리들은 산점도, 선 그래프, 막대 그래프, 히스토그램, 히트맵 등 다양한 시각화를 만들기 위해 사용됩니다. 다양한 커스터마이징 옵션을 제공하며, 사용자들이 보기 좋고 정보적인 그래프를 만들어 복잡한 데이터 세

3.3.2 davinci-instruct-beta

InstructGPT davinci-instruct-beta 모형에 넣어 번역 품질을 살펴보자.

davinci_instruct_text = translate_by_instruct_model(source_text, 'davinci-instruct-beta', 'English', 'Korean')
davinci_instruct_text <- glue::glue("{py$davinci_instruct_text['choices'][[1]]$text}\n")
davinci_instruct_text %>% 
  write_lines("data/davinci_instruct_text.txt")
davinci_instruct_text <- read_lines("data/davinci_instruct_text.txt")
cat(str_c(davinci_instruct_text, collapse = "\n"))
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파일을 읽어주세요.
#> 
#> 여러번째 파

참고문헌

Marie, B. (2022). Translate with GPT-3 - Machine translation but without a machine translation system. Towards Data Science. https://medium.com/towards-data-science/translate-with-gpt-3-9903c4a6f385
Marie, B. (2023). ChatGPT to Evaluate Generated Text How good is ChatGPT for evaluating automatic summarization, story generation, and data-to-text generation? In Towards Data Science. https://medium.com/mlearning-ai/chatgpt-to-evaluate-generated-text-48b42f39086e