go openai

2025-12-11 0 252

go openai

该库为OpenAI API提供了非正式的GO客户。我们支持:

  • Chatgpt 4o,O1
  • GPT-3,GPT-4
  • dall·e 2,dall·e 3,gpt图像1
  • 耳语

安装

go get github.com/sashabaranov/go-openai

当前,Go-Openai需要GO版本1.18或更高版本。

用法

chatgpt示例用法:

 package main

import (
	\"context\"
	\"fmt\"
	openai \"github.com/sashabaranov/go-openai\"
)

func main () {
	client := openai . NewClient ( \"your token\" )
	resp , err := client . CreateChatCompletion (
		context . Background (),
		openai. ChatCompletionRequest {
			Model : openai . GPT3Dot5Turbo ,
			Messages : []openai. ChatCompletionMessage {
				{
					Role :    openai . ChatMessageRoleUser ,
					Content : \"Hello!\" ,
				},
			},
		},
	)

	if err != nil {
		fmt . Printf ( \"ChatCompletion error: %v \\n \" , err )
		return
	}

	fmt . Println ( resp . Choices [ 0 ]. Message . Content )
}

获取OpenAI API密钥:

  1. 访问OpenAI网站https://platform.ope**n*ai.com/account/api-keys。
  2. 如果您没有帐户,请单击“注册”以创建一个。如果这样做,请单击“登录”。
  3. 登录后,请导航到您的API密钥管理页面。
  4. 单击“创建新的秘密密钥”。
  5. 输入新密钥的名称,然后单击“创建秘密键”。
  6. 将显示您的新API键。使用此键与OpenAI API进行交互。

注意:您的API密钥是敏感信息。不要与任何人分享。

其他示例:

CHATGPT流媒体完成
 package main

import (
	\"context\"
	\"errors\"
	\"fmt\"
	\"io\"
	openai \"github.com/sashabaranov/go-openai\"
)

func main () {
	c := openai . NewClient ( \"your token\" )
	ctx := context . Background ()

	req := openai. ChatCompletionRequest {
		Model :     openai . GPT3Dot5Turbo ,
		MaxTokens : 20 ,
		Messages : []openai. ChatCompletionMessage {
			{
				Role :    openai . ChatMessageRoleUser ,
				Content : \"Lorem ipsum\" ,
			},
		},
		Stream : true ,
	}
	stream , err := c . CreateChatCompletionStream ( ctx , req )
	if err != nil {
		fmt . Printf ( \"ChatCompletionStream error: %v \\n \" , err )
		return
	}
	defer stream . Close ()

	fmt . Printf ( \"Stream response: \" )
	for {
		response , err := stream . Recv ()
		if errors . Is ( err , io . EOF ) {
			fmt . Println ( \" \\n Stream finished\" )
			return
		}

		if err != nil {
			fmt . Printf ( \" \\n Stream error: %v \\n \" , err )
			return
		}

		fmt . Printf ( response . Choices [ 0 ]. Delta . Content )
	}
}
GPT-3完成
 package main

import (
	\"context\"
	\"fmt\"
	openai \"github.com/sashabaranov/go-openai\"
)

func main () {
	c := openai . NewClient ( \"your token\" )
	ctx := context . Background ()

	req := openai. CompletionRequest {
		Model :     openai . GPT3Babbage002 ,
		MaxTokens : 5 ,
		Prompt :    \"Lorem ipsum\" ,
	}
	resp , err := c . CreateCompletion ( ctx , req )
	if err != nil {
		fmt . Printf ( \"Completion error: %v \\n \" , err )
		return
	}
	fmt . Println ( resp . Choices [ 0 ]. Text )
}
GPT-3流媒体完成
 package main

import (
	\"errors\"
	\"context\"
	\"fmt\"
	\"io\"
	openai \"github.com/sashabaranov/go-openai\"
)

func main () {
	c := openai . NewClient ( \"your token\" )
	ctx := context . Background ()

	req := openai. CompletionRequest {
		Model :     openai . GPT3Babbage002 ,
		MaxTokens : 5 ,
		Prompt :    \"Lorem ipsum\" ,
		Stream :    true ,
	}
	stream , err := c . CreateCompletionStream ( ctx , req )
	if err != nil {
		fmt . Printf ( \"CompletionStream error: %v \\n \" , err )
		return
	}
	defer stream . Close ()

	for {
		response , err := stream . Recv ()
		if errors . Is ( err , io . EOF ) {
			fmt . Println ( \"Stream finished\" )
			return
		}

		if err != nil {
			fmt . Printf ( \"Stream error: %v \\n \" , err )
			return
		}


		fmt . Printf ( \"Stream response: %v \\n \" , response )
	}
}
音频语音到文本
 package main

import (
	\"context\"
	\"fmt\"

	openai \"github.com/sashabaranov/go-openai\"
)

func main () {
	c := openai . NewClient ( \"your token\" )
	ctx := context . Background ()

	req := openai. AudioRequest {
		Model :    openai . Whisper1 ,
		FilePath : \"recording.mp3\" ,
	}
	resp , err := c . CreateTranscription ( ctx , req )
	if err != nil {
		fmt . Printf ( \"Transcription error: %v \\n \" , err )
		return
	}
	fmt . Println ( resp . Text )
}
音频字幕
 package main

import (
	\"context\"
	\"fmt\"
	\"os\"

	openai \"github.com/sashabaranov/go-openai\"
)

func main () {
	c := openai . NewClient ( os . Getenv ( \"OPENAI_KEY\" ))

	req := openai. AudioRequest {
		Model :    openai . Whisper1 ,
		FilePath : os . Args [ 1 ],
		Format :   openai . AudioResponseFormatSRT ,
	}
	resp , err := c . CreateTranscription ( context . Background (), req )
	if err != nil {
		fmt . Printf ( \"Transcription error: %v \\n \" , err )
		return
	}
	f , err := os . Create ( os . Args [ 1 ] + \".srt\" )
	if err != nil {
		fmt . Printf ( \"Could not open file: %v \\n \" , err )
		return
	}
	defer f . Close ()
	if _ , err := f . WriteString ( resp . Text ); err != nil {
		fmt . Printf ( \"Error writing to file: %v \\n \" , err )
		return
	}
}
dall-e 2图像生成
 package main

import (
	\"bytes\"
	\"context\"
	\"encoding/base64\"
	\"fmt\"
	openai \"github.com/sashabaranov/go-openai\"
	\"image/png\"
	\"os\"
)

func main () {
	c := openai . NewClient ( \"your token\" )
	ctx := context . Background ()

	// Sample image by link
	reqUrl := openai. ImageRequest {
		Prompt :         \"Parrot on a skateboard performs a trick, cartoon style, natural light, high detail\" ,
		Size :           openai . CreateImageSize256x256 ,
		ResponseFormat : openai . CreateImageResponseFormatURL ,
		N :              1 ,
	}

	respUrl , err := c . CreateImage ( ctx , reqUrl )
	if err != nil {
		fmt . Printf ( \"Image creation error: %v \\n \" , err )
		return
	}
	fmt . Println ( respUrl . Data [ 0 ]. URL )

	// Example image as base64
	reqBase64 := openai. ImageRequest {
		Prompt :         \"Portrait of a humanoid parrot in a classic costume, high detail, realistic light, unreal engine\" ,
		Size :           openai . CreateImageSize256x256 ,
		ResponseFormat : openai . CreateImageResponseFormatB64JSON ,
		N :              1 ,
	}

	respBase64 , err := c . CreateImage ( ctx , reqBase64 )
	if err != nil {
		fmt . Printf ( \"Image creation error: %v \\n \" , err )
		return
	}

	imgBytes , err := base64 . StdEncoding . DecodeString ( respBase64 . Data [ 0 ]. B64JSON )
	if err != nil {
		fmt . Printf ( \"Base64 decode error: %v \\n \" , err )
		return
	}

	r := bytes . NewReader ( imgBytes )
	imgData , err := png . Decode ( r )
	if err != nil {
		fmt . Printf ( \"PNG decode error: %v \\n \" , err )
		return
	}

	file , err := os . Create ( \"example.png\" )
	if err != nil {
		fmt . Printf ( \"File creation error: %v \\n \" , err )
		return
	}
	defer file . Close ()

	if err := png . Encode ( file , imgData ); err != nil {
		fmt . Printf ( \"PNG encode error: %v \\n \" , err )
		return
	}

	fmt . Println ( \"The image was saved as example.png\" )
}
GPT图像1图像生成
 package main

import (
	\"context\"
	\"encoding/base64\"
	\"fmt\"
	\"os\"

	openai \"github.com/sashabaranov/go-openai\"
)

func main () {
	c := openai . NewClient ( \"your token\" )
	ctx := context . Background ()

	req := openai. ImageRequest {
		Prompt :            \"Parrot on a skateboard performing a trick. Large bold text \\\" SKATE MASTER \\\" banner at the bottom of the image. Cartoon style, natural light, high detail, 1:1 aspect ratio.\" ,
		Background :        openai . CreateImageBackgroundOpaque ,
		Model :             openai . CreateImageModelGptImage1 ,
		Size :              openai . CreateImageSize1024x1024 ,
		N :                 1 ,
		Quality :           openai . CreateImageQualityLow ,
		OutputCompression : 100 ,
		OutputFormat :      openai . CreateImageOutputFormatJPEG ,
		// Moderation: 		 openai.CreateImageModerationLow,
		// User: 					 \"\",
	}

	resp , err := c . CreateImage ( ctx , req )
	if err != nil {
		fmt . Printf ( \"Image creation Image generation with GPT Image 1error: %v \\n \" , err )
		return
	}

	fmt . Println ( \"Image Base64:\" , resp . Data [ 0 ]. B64JSON )

	// Decode the base64 data
	imgBytes , err := base64 . StdEncoding . DecodeString ( resp . Data [ 0 ]. B64JSON )
	if err != nil {
		fmt . Printf ( \"Base64 decode error: %v \\n \" , err )
		return
	}

	// Write image to file
	outputPath := \"generated_image.jpg\"
	err = os . WriteFile ( outputPath , imgBytes , 0644 )
	if err != nil {
		fmt . Printf ( \"Failed to write image file: %v \\n \" , err )
		return
	}

	fmt . Printf ( \"The image was saved as %s \\n \" , outputPath )
}
配置代理
 config := openai . DefaultConfig ( \"token\" )
proxyUrl , err := url . Parse ( \"http://localhost:{port}\" )
if err != nil {
	panic ( err )
}
transport := & http. Transport {
	Proxy : http . ProxyURL ( proxyUrl ),
}
config . HTTPClient = & http. Client {
	Transport : transport ,
}

c := openai . NewClientWithConfig ( config )

另请参阅:https://pkg.go.dev/github.com/sashabaranov/go-openai#clientconfig

CHATGPT支持上下文
 package main

import (
	\"bufio\"
	\"context\"
	\"fmt\"
	\"os\"
	\"strings\"

	\"github.com/sashabaranov/go-openai\"
)

func main () {
	client := openai . NewClient ( \"your token\" )
	messages := make ([]openai. ChatCompletionMessage , 0 )
	reader := bufio . NewReader ( os . Stdin )
	fmt . Println ( \"Conversation\" )
	fmt . Println ( \"---------------------\" )

	for {
		fmt . Print ( \"-> \" )
		text , _ := reader . ReadString ( \'\\n\' )
		// convert CRLF to LF
		text = strings . Replace ( text , \" \\n \" , \"\" , - 1 )
		messages = append ( messages , openai. ChatCompletionMessage {
			Role :    openai . ChatMessageRoleUser ,
			Content : text ,
		})

		resp , err := client . CreateChatCompletion (
			context . Background (),
			openai. ChatCompletionRequest {
				Model :    openai . GPT3Dot5Turbo ,
				Messages : messages ,
			},
		)

		if err != nil {
			fmt . Printf ( \"ChatCompletion error: %v \\n \" , err )
			continue
		}

		content := resp . Choices [ 0 ]. Message . Content
		messages = append ( messages , openai. ChatCompletionMessage {
			Role :    openai . ChatMessageRoleAssistant ,
			Content : content ,
		})
		fmt . Println ( content )
	}
}
Azure Openai Chatgpt
 package main

import (
	\"context\"
	\"fmt\"

	openai \"github.com/sashabaranov/go-openai\"
)

func main () {
	config := openai . DefaultAzureConfig ( \"your Azure OpenAI Key\" , \"https://*yo**ur Azure OpenAI Endpoint\" )
	// If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function
	// config.AzureModelMapperFunc = func(model string) string {
	// 	azureModelMapping := map[string]string{
	// 		\"gpt-3.5-turbo\": \"your gpt-3.5-turbo deployment name\",
	// 	}
	// 	return azureModelMapping[model]
	// }

	client := openai . NewClientWithConfig ( config )
	resp , err := client . CreateChatCompletion (
		context . Background (),
		openai. ChatCompletionRequest {
			Model : openai . GPT3Dot5Turbo ,
			Messages : []openai. ChatCompletionMessage {
				{
					Role :    openai . ChatMessageRoleUser ,
					Content : \"Hello Azure OpenAI!\" ,
				},
			},
		},
	)
	if err != nil {
		fmt . Printf ( \"ChatCompletion error: %v \\n \" , err )
		return
	}

	fmt . Println ( resp . Choices [ 0 ]. Message . Content )
}
嵌入语义相似性
 package main

import (
	\"context\"
	\"log\"
	openai \"github.com/sashabaranov/go-openai\"

)

func main () {
	client := openai . NewClient ( \"your-token\" )

	// Create an EmbeddingRequest for the user query
	queryReq := openai. EmbeddingRequest {
		Input : [] string { \"How many chucks would a woodchuck chuck\" },
		Model : openai . AdaEmbeddingV2 ,
	}

	// Create an embedding for the user query
	queryResponse , err := client . CreateEmbeddings ( context . Background (), queryReq )
	if err != nil {
		log . Fatal ( \"Error creating query embedding:\" , err )
	}

	// Create an EmbeddingRequest for the target text
	targetReq := openai. EmbeddingRequest {
		Input : [] string { \"How many chucks would a woodchuck chuck if the woodchuck could chuck wood\" },
		Model : openai . AdaEmbeddingV2 ,
	}

	// Create an embedding for the target text
	targetResponse , err := client . CreateEmbeddings ( context . Background (), targetReq )
	if err != nil {
		log . Fatal ( \"Error creating target embedding:\" , err )
	}

	// Now that we have the embeddings for the user query and the target text, we
	// can calculate their similarity.
	queryEmbedding := queryResponse . Data [ 0 ]
	targetEmbedding := targetResponse . Data [ 0 ]

	similarity , err := queryEmbedding . DotProduct ( & targetEmbedding )
	if err != nil {
		log . Fatal ( \"Error calculating dot product:\" , err )
	}

	log . Printf ( \"The similarity score between the query and the target is %f\" , similarity )
}
Azure Openai嵌入
 package main

import (
	\"context\"
	\"fmt\"

	openai \"github.com/sashabaranov/go-openai\"
)

func main () {

	config := openai . DefaultAzureConfig ( \"your Azure OpenAI Key\" , \"https://*yo**ur Azure OpenAI Endpoint\" )
	config . APIVersion = \"2023-05-15\" // optional update to latest API version

	//If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function
	//config.AzureModelMapperFunc = func(model string) string {
	//    azureModelMapping := map[string]string{
	//        \"gpt-3.5-turbo\":\"your gpt-3.5-turbo deployment name\",
	//    }
	//    return azureModelMapping[model]
	//}

	input := \"Text to vectorize\"

	client := openai . NewClientWithConfig ( config )
	resp , err := client . CreateEmbeddings (
		context . Background (),
		openai. EmbeddingRequest {
			Input : [] string { input },
			Model : openai . AdaEmbeddingV2 ,
		})

	if err != nil {
		fmt . Printf ( \"CreateEmbeddings error: %v \\n \" , err )
		return
	}

	vectors := resp . Data [ 0 ]. Embedding // []float32 with 1536 dimensions

	fmt . Println ( vectors [: 10 ], \"...\" , vectors [ len ( vectors ) - 10 :])
}
JSON策略用于调用功能

现在,聊天完成可以选择拨打函数以获取更多信息(请参阅此处的开发人员文档)。

为了描述可以调用的函数的类型,必须提供JSON模式。许多JSON模式库存在,并且比我们在此库中提供的更先进,但是我们为那些想使用此功能而无需格式化自己的JSON模式有效负载的人提供了一个简单的JSonschema软件包。

开发人员文档将此JSON模式定义为一个例子:

{
  \"name\" : \" get_current_weather \" ,
  \"description\" : \" Get the current weather in a given location \" ,
  \"parameters\" :{
    \"type\" : \" object \" ,
    \"properties\" :{
        \"location\" :{
          \"type\" : \" string \" ,
          \"description\" : \" The city and state, e.g. San Francisco, CA \"
        },
        \"unit\" :{
          \"type\" : \" string \" ,
          \"enum\" :[
              \" celsius \" ,
              \" fahrenheit \"
          ]
        }
    },
    \"required\" :[
        \" location \"
    ]
  }
}

使用Jsonschema软件包,可以使用结构这样创建此架构:

 FunctionDefinition {
  Name : \"get_current_weather\" ,
  Parameters : jsonschema. Definition {
    Type : jsonschema . Object ,
    Properties : map [ string ]jsonschema. Definition {
      \"location\" : {
        Type : jsonschema . String ,
        Description : \"The city and state, e.g. San Francisco, CA\" ,
      },
      \"unit\" : {
        Type : jsonschema . String ,
        Enum : [] string { \"celsius\" , \"fahrenheit\" },
      },
    },
    Required : [] string { \"location\" },
  },
}

函数定义的参数字段可以接受上述样式,甚至可以从另一个库中嵌套结构(只要可以将其编码为JSON)即可。

错误处理

Open-AI保留有关如何处理API错误的清晰文档

例子:

e := &openai.APIError{}
if errors.As(err, &e) {
  switch e.HTTPStatusCode {
    case 401:
      // invalid auth or key (do not retry)
    case 429:
      // rate limiting or engine overload (wait and retry) 
    case 500:
      // openai server error (retry)
    default:
      // unhandled
  }
}

微调模型

下载源码

通过命令行克隆项目:

git clone https://github.com/sashabaranov/go-openai.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 建站资源 go openai https://www.zuozi.net/35483.html

llama gpt
上一篇: llama gpt
CL4R1T4S
下一篇: CL4R1T4S
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务