使用 Amazon Titan Text Premier 模型在 Go 中构建生成式 AI 应用程序
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《使用 Amazon Titan Text Premier 模型在 Go 中构建生成式 AI 应用程序》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
将 amazon titan text premier 模型与 langchaingo 包结合使用
在本博客中,我将引导您了解如何通过 langchaingo 在 go 应用程序中使用 amazon titan text premier 模型,langchaingo 是 langchain 的 go 端口(最初是为 python 和 js/ts 编写的)。
amazon titan text premier 是 amazon titan text 系列中的高级法学硕士。它适用于各种任务,包括 rag、代理、聊天、思想链、开放式文本生成、头脑风暴、总结、代码生成、表格创建、数据格式化、释义、重写、提取和问答。 titan text premier 还针对与 amazon bedrock 的代理和知识库集成进行了优化。
将 titan text premier 与 langchaingo 结合使用
让我们从一个例子开始。
请参阅本博文中的**开始之前*部分,以完成运行示例的先决条件。这包括安装 go、配置 amazon bedrock 访问以及提供必要的 iam 权限。*
完整代码可以参考这里。运行示例:
git clone https://github.com/abhirockzz/titan-premier-bedrock-go cd titan-premier-bedrock-go go run basic/main.go
针对“用 100 个单词或更少的内容解释 ai”提示,我得到了以下回复,但您的情况可能有所不同:
artificial intelligence (ai) is a branch of computer science that focuses on creating intelligent machines that can think, learn, and act like humans. it uses advanced algorithms and machine learning techniques to enable computers to recognize patterns, make decisions, and solve problems. ai has the potential to revolutionize various industries, including healthcare, finance, transportation, and entertainment, by automating tasks, improving efficiency, and providing personalized experiences. however, there are also concerns about the ethical implications and potential risks associated with ai, such as job displacement, privacy, and bias.
以下是代码的快速浏览:
我们首先实例化 bedrockruntime.client:
cfg, err := config.loaddefaultconfig(context.background(), config.withregion(region)) client = bedrockruntime.newfromconfig(cfg)
我们使用它来创建 langchaingo llm.model 实例 - 请注意,我们指定的 modelid 是 titan text premier 的 modelid,即 amazon.titan-text-premier-v1:0。
llm, err := bedrock.new(bedrock.withclient(client), bedrock.withmodel(modelid))
我们创建一个 llms.messagecontent,llm 调用由 llm.generatecontent 完成。请注意,您不必考虑 titan text premier 特定请求/响应有效负载 - 这是由 langchaingo 抽象的:
msg := []llms.messagecontent{
{
role: llms.chatmessagetypehuman,
parts: []llms.contentpart{
llms.textpart("explain ai in 100 words or less."),
},
},
}
resp, err := llm.generatecontent(context.background(), msg, llms.withmaxtokens(maxtokencountlimitfortitantextpremier))
与您的文档聊天
这也是一个很常见的场景。 langchaingo 支持多种类型,包括文本、pdf、html(甚至 notion!)。
完整代码可以参考这里。要运行此示例:
go run doc-chat/main.go
该示例使用 amazon bedrock 用户指南中的此页面作为源文档 (html),但您可以随意使用任何其他来源:
export source_url=<enter url> go run doc-chat/main.go
系统应该提示您输入问题:
loaded content from https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html enter your message:
我尝试了这些问题并得到了相当准确的答案:
1. tell me the names of the supported providers 2. tell me the model id for titan text premier 3. give me the list of amazon titan family of models 4. what is the titan text premier model id for a provisioned throughput customer?

顺便说一下,amazon bedrock 本身也提供了“与文档聊天”功能。
让我们快速浏览一下代码。我们首先从源 url 加载内容:
func getdocs(link string) []schema.document {
//...
resp, err := http.get(link)
docs, err := documentloaders.newhtml(resp.body).load(context.background())
return docs
}
然后,我们使用简单的 for 循环开始对话:
//...
for {
fmt.print("\nenter your message: ")
input, _ := reader.readstring('\n')
input = strings.trimspace(input)
answer, err := chains.call(
context.background(),
docchainwithcustomprompt(llm),
map[string]any{
"input_documents": docs,
"question": input,
},
chains.withmaxtokens(maxtokencountlimitfortitantextpremier))
//...
}
我们使用的链是使用自定义提示创建的(基于此指南) - 我们覆盖 langchaingo 中的默认行为:
func docchainwithcustomprompt(llm *bedrock_llm.llm) chains.chain {
ragprompttemplate := prompts.newprompttemplate(
prompttemplatestring,
[]string{"context", "question"},
)
qapromptselector := chains.conditionalpromptselector{
defaultprompt: ragprompttemplate,
}
prompt := qapromptselector.getprompt(llm)
llmchain := chains.newllmchain(llm, prompt)
return chains.newstuffdocuments(llmchain)
}
现在来看最后一个例子 - 另一个流行的用例。
rag - 检索增强生成
我之前介绍过如何在 go 应用程序中使用 rag。这次我们将使用:
- titan text premier 作为法学硕士,
- titan embeddings g1 作为嵌入模型,
- 使用 pgvector 扩展将 postgresql 作为向量存储(使用 docker 在本地运行)。
启动 docker 容器:
docker run --name pgvector --rm -it -p 5432:5432 -e postgres_user=postgres -e postgres_password=postgres ankane/pgvector
通过从不同的终端登录 postgresql(使用 psql)来激活 pgvector 扩展:
# enter postgres when prompted for password psql -h localhost -u postgres -w create extension if not exists vector;
完整代码可以参考这里。要运行此示例:
go run rag/main.go
该示例使用 amazon bedrock studio 页面作为源文档 (html),但您可以随意使用任何其他源:
export source_url=<enter url> go run rag/main.go
您应该看到输出,并提示输入您的问题。我尝试过这些:
what is bedrock studio? how do i enable bedrock studio?

像往常一样,让我们看看发生了什么。数据加载的方式与以前类似,对话也同样如此(for 循环):
for {
fmt.print("\nenter your message: ")
question, _ := reader.readstring('\n')
question = strings.trimspace(question)
result, err := chains.run(
context.background(),
retrievalqachainwithcustomprompt(llm, vectorstores.toretriever(store, numofresults)),
question,
chains.withmaxtokens(maxtokencountlimitfortitantextpremier),
)
//....
}
rag 部分略有不同。我们使用带有自定义提示的 retrievelqa 链(类似于 amazon bedrock 知识库使用的提示):
func retrievalQAChainWithCustomPrompt(llm *bedrock_llm.LLM, retriever vectorstores.Retriever) chains.Chain {
ragPromptTemplate := prompts.NewPromptTemplate(
ragPromptTemplateString,
[]string{"context", "question"},
)
qaPromptSelector := chains.ConditionalPromptSelector{
DefaultPrompt: ragPromptTemplate,
}
prompt := qaPromptSelector.GetPrompt(llm)
llmChain := chains.NewLLMChain(llm, prompt)
stuffDocsChain := chains.NewStuffDocuments(llmChain)
return chains.NewRetrievalQA(
stuffDocsChain,
retriever,
)
}
结论
我介绍了 amazon titan text premier,它是 titan 系列中的多种文本生成模型之一。除了文本生成之外,amazon titan 还具有嵌入模型(文本和多模式)和图像生成。您可以通过探索 amazon bedrock 文档中的所有内容来了解更多信息。快乐建造!
终于介绍完啦!小伙伴们,这篇关于《使用 Amazon Titan Text Premier 模型在 Go 中构建生成式 AI 应用程序》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
Day 组件和 Props - ReactJS
- 上一篇
- Day 组件和 Props - ReactJS
- 下一篇
- 中国科学院大连化物所等开发出用于电池寿命预测的深度学习模型
-
- Golang · Go教程 | 4小时前 |
- Go语言实现与外部程序持续通信技巧
- 229浏览 收藏
-
- Golang · Go教程 | 4小时前 |
- GolangWeb错误处理技巧分享
- 190浏览 收藏
-
- Golang · Go教程 | 4小时前 |
- Go语言error接口错误返回实例解析
- 324浏览 收藏
-
- Golang · Go教程 | 4小时前 |
- Golang模板方法模式实战解析
- 180浏览 收藏
-
- Golang · Go教程 | 4小时前 | golang dockercompose 健康检查 多阶段构建 启动优化
- Golang优化Docker多容器启动技巧
- 228浏览 收藏
-
- Golang · Go教程 | 4小时前 |
- 优化Golang模块缓存,提升构建效率技巧
- 483浏览 收藏
-
- Golang · Go教程 | 4小时前 |
- Go递归函数返回值处理方法
- 353浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golang微服务容器化部署指南
- 226浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Golang静态资源管理实战指南
- 186浏览 收藏
-
- Golang · Go教程 | 5小时前 | golang 自定义函数 模板渲染 html/template 模板语法
- Golang模板渲染教程与使用详解
- 104浏览 收藏
-
- Golang · Go教程 | 5小时前 |
- Go模块版本管理全攻略
- 268浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ChatExcel酷表
- ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
- 3182次使用
-
- Any绘本
- 探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
- 3393次使用
-
- 可赞AI
- 可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
- 3424次使用
-
- 星月写作
- 星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
- 4528次使用
-
- MagicLight
- MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
- 3802次使用
-
- Golangmap实践及实现原理解析
- 2022-12-28 505浏览
-
- go和golang的区别解析:帮你选择合适的编程语言
- 2023-12-29 503浏览
-
- 试了下Golang实现try catch的方法
- 2022-12-27 502浏览
-
- 如何在go语言中实现高并发的服务器架构
- 2023-08-27 502浏览
-
- 提升工作效率的Go语言项目开发经验分享
- 2023-11-03 502浏览

