Git Product home page Git Product logo

k-openai's Introduction

K-Openai

K-openai는 OpenAI의 API 사용을 위한 프롬프트 작성 시 파파고 번역기능을 통해 Token 사용량 최소화와 서비스 형태에 맞는 응답을 하기 위해 제작하게 되었습니다.

목차

사전 준비

  • go version >= 1.18 이상 설치 되어 있어야합니다.
  • OpenAI 에서 API Key를 발급 받아야합니다.
  • Naver Developers에서 client를 발급 받아야합니다.

설치 방법

터미널 또는 명령 프롬프트를 열고 다음 명령어를 실행하여 의존성을 설치합니다.

go get -u github.com/bluecheat/[email protected]

Config 설정 방법

1. yaml 파일을 읽어서 설정하는 방법

config, err := kopenai.LoadConfiguration("./kopenai_env.yml")
if err != nil {
    log.Fatal(err)
}

2. 변수 직접 선언 방법

config := kopenai.Config{
    Openai: kopenai.OpenAiConfig{
        ApiKey: "apikey",
    },
    Naver: kopenai.NaverOpenApiConfig{
        ClientId:     "clinetId",
        ClientSecret: "clientSecret",
    },
}

사용 방법

1. Chat API kopenai.Chat ( GPT 채팅 API )

Chat API에서 사용하는 요청값, 응답값은 동일하고 추가적으로 번역 옵션을 넣어서 처리합니다.

파라미터 정보

  • openai.ChatCompletionRequest: 기존의 openai의 Chat 호출 시 사용되는 요청값
  • kopenai.ChatTransOption: 프롬프트 번역 시 사용하는 Option
    • InputPrompt: 프롬프트에 대한 번역 ( source: 기존 언어, target: 번역할 언어 )
    • OutputPrompt: 프롬프트 결과에 대한 번역 ( source: 기존 언어, target: 번역할 언어 )

예제 코드

...

client := kopenai.NewKopenAiGpt(config)

ctx := context.Background()
resp, err := client.Chat(ctx, openai.ChatCompletionRequest{
    Model: openai.GPT3Dot5Turbo,
    Messages: []openai.ChatCompletionMessage{
        {
            Role:    openai.ChatMessageRoleUser,
            Content: "안녕 너의 이름은 뭐니?",
        },
    },
}, kopenai.ChatTransOption{
    InputPrompt: &kopenai.TransOption{
        Source: kopenai.KO,
        Target: kopenai.EN,
    },
    OutputPrompt: &kopenai.TransOption{
        Source: kopenai.EN,
        Target: kopenai.KO,
    },
})
if err != nil {
    t.Error(err)
    return
}
fmt.Println(resp.Choices[0].Message)

2. ImageGenerate API kopenai.ImageGenerate ( 이미지 생성 API )

ImageGenerate API에서 사용하는 요청값, 응답값은 동일하고 추가적으로 번역 옵션을 넣어서 처리합니다.

파라미터 정보

  • openai.ImageRequest: 기존의 openai의 ImageGenerate 호출 시 사용되는 요청값
  • kopenai.ImageTransOption: 프롬프트 번역 시 사용하는 Option
    • InputPrompt: 프롬프트에 대한 번역 ( source: 기존 언어, target: 번역할 언어 )

예제 코드

...
client := kopenai.NewKopenAiGpt(config)

ctx := context.Background()
resp, err := client.ImageGenerate(ctx, openai.ImageRequest{
    Prompt:         "업무, 개발, AI개발",
    N:              1,
    Size:           openai.CreateImageSize256x256,
    ResponseFormat: openai.CreateImageResponseFormatB64JSON,
}, kopenai.ImageTransOption{
    InputPrompt: &kopenai.TransOption{
        Source: kopenai.KO,
        Target: kopenai.EN,
    },
})
if err != nil {
    t.Error(err)
    return
}

// Open output file
dec, err := base64.StdEncoding.DecodeString(resp.Data[0].B64JSON)
if err != nil {
    panic(err)
}
output, err := os.Create("created-image-" + time.Now().UTC().String() + ".png")
if err != nil {
    panic(err)
}
// Close output file
defer output.Close()

if _, err := output.Write(dec); err != nil {
    panic(err)
}
if err := output.Sync(); err != nil {
    panic(err)
}

이미지 생성 결과

image

k-openai's People

Contributors

bluecheat avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.