Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# OPENAI API 访问密钥配置
OPENAI_API_KEY = ""

# 文心 API 访问密钥配置
# 方式1. 使用应用 AK/SK 鉴权
# 创建的应用的 API Key
Expand All @@ -16,19 +15,11 @@ QIANFAN_SECRET_KEY = ""
# Ernie SDK 文心 API 访问密钥配置
EB_ACCESS_TOKEN = ""

# 控制台中获取的 APPID 信息
SPARK_APPID = ""
# 控制台中获取的 APIKey 信息
SPARK_API_KEY = ""
# 控制台中获取的 APISecret 信息
SPARK_API_SECRET = ""

# langchain中星火 API 访问密钥配置
# 控制台中获取的 APPID 信息
IFLYTEK_SPARK_APP_ID = ""
# 控制台中获取的 APISecret 信息
IFLYTEK_SPARK_API_KEY = ""
# 控制台中获取的 APIKey 信息
IFLYTEK_SPARK_API_KEY = ""
# 控制台中获取的 APISecret 信息
IFLYTEK_SPARK_API_SECRET = ""

# 智谱 API 访问密钥配置
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ cython_debug/
*/.DS_Store
*/*/.DS_Store
.idea/
.DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified data_base/vector_db/chroma/chroma.sqlite3
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
416 changes: 76 additions & 340 deletions notebook/C2 使用 LLM API 开发应用/2. 使用 LLM API.ipynb

Large diffs are not rendered by default.

158 changes: 100 additions & 58 deletions notebook/C2 使用 LLM API 开发应用/3. Prompt Engineering.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -102,11 +102,11 @@
"\n",
"# 一个封装 OpenAI 接口的函数,参数为 Prompt,返回对应结果\n",
"def get_completion(prompt,\n",
" model=\"gpt-3.5-turbo\"\n",
" model=\"gpt-4o\"\n",
" ):\n",
" '''\n",
" prompt: 对应的提示词\n",
" model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT)。你也可以选择其他模型。\n",
" model: 调用的模型,默认为 gpt-4o。你也可以选择其他模型。\n",
" https://platform.openai.com/docs/models/overview\n",
" '''\n",
"\n",
Expand All @@ -131,14 +131,14 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"请回答问题:你是谁\n"
"询问对方身份。\n"
]
}
],
Expand Down Expand Up @@ -177,14 +177,14 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"我是一个智能助手。\n"
"我是一个由OpenAI开发的AI助手,旨在提供信息和帮助。\n"
]
}
],
Expand Down Expand Up @@ -223,7 +223,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand All @@ -233,22 +233,22 @@
"```json\n",
"[\n",
" {\n",
" \"book_id\": 1,\n",
" \"title\": \"幻境之门\",\n",
" \"author\": \"张三\",\n",
" \"genre\": \"奇幻\"\n",
" \"book_id\": \"001\",\n",
" \"title\": \"星河彼岸\",\n",
" \"author\": \"李明宇\",\n",
" \"genre\": \"科幻\"\n",
" },\n",
" {\n",
" \"book_id\": 2,\n",
" \"title\": \"星际迷航\",\n",
" \"author\": \"李四\",\n",
" \"genre\": \"科幻\"\n",
" \"book_id\": \"002\",\n",
" \"title\": \"古城谜影\",\n",
" \"author\": \"王晓峰\",\n",
" \"genre\": \"悬疑\"\n",
" },\n",
" {\n",
" \"book_id\": 3,\n",
" \"title\": \"时光漩涡\",\n",
" \"author\": \"王五\",\n",
" \"genre\": \"穿越\"\n",
" \"book_id\": \"003\",\n",
" \"title\": \"心灵之旅\",\n",
" \"author\": \"陈静\",\n",
" \"genre\": \"心理\"\n",
" }\n",
"]\n",
"```\n"
Expand Down Expand Up @@ -286,21 +286,21 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Text 1 的总结:\n",
"第一步 - 把水烧开。\n",
"第二步 - 拿一个杯子并把茶包放进去。\n",
"第三步 - 把烧开的水倒在茶包上。\n",
"第四步 - 等待一会儿,让茶叶浸泡。\n",
"第五步 - 取出茶包。\n",
"第六步 - 如果愿意,可以加一些糖或牛奶调味。\n",
"第七步 - 尽情享受一杯美味的茶。\n"
"第一步 - 把水烧开。 \n",
"第二步 - 在等待期间,拿一个杯子并把茶包放进去。 \n",
"第三步 - 一旦水足够热,就把它倒在茶包上。 \n",
"第四步 - 等待一会儿,让茶叶浸泡。 \n",
"第五步 - 几分钟后,取出茶包。 \n",
"第六步 - 如果您愿意,可以加一些糖或牛奶调味。 \n",
"第七步 - 享受一杯美味的茶。\n"
]
}
],
Expand Down Expand Up @@ -342,7 +342,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -398,14 +398,14 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<圣贤>: 孝顺者,孝敬父母,顺从长辈,尊重家族传统,忠诚孝道,不忘家国情怀。\n"
"<圣贤>: 夫孝,德之本也,教之所由生也。孝者,善事父母者也。事亲以敬,养亲以乐,终亲之年,毋使有憾。孝顺者,心存敬爱,行以奉养,始终如一,方为至孝。\n"
]
}
],
Expand Down Expand Up @@ -472,19 +472,21 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"response :\n",
"摘要:在一个迷人的村庄里,兄妹杰克和吉尔出发去一个山顶井里打水,不幸中途发生意外,但他们仍然充满冒险精神。\n",
"摘要:杰克和吉尔在村庄里去山顶打水时摔倒,但他们的冒险精神未减。\n",
"\n",
"翻译:In a charming village, siblings Jack and Jill set out to fetch water from a well on top of a hill, unfortunately encountering an accident along the way, but their adventurous spirit remains undiminished.\n",
"翻译:Jack and Jill fell while fetching water from a hilltop in the village, but their adventurous spirit remained undiminished.\n",
"\n",
"名称:Jack, Jill\n"
"名称:[\"Jack\", \"Jill\"]\n",
"\n",
"输出 JSON 格式:{\"English_summary\": \"Jack and Jill fell while fetching water from a hilltop in the village, but their adventurous spirit remained undiminished.\", \"num_names\": 2}\n"
]
}
],
Expand Down Expand Up @@ -539,14 +541,36 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"学生的解决方案是正确的。首年运营的总费用为450x+100,000美元,其中x为发电站的大小,单位为平方英尺。\n"
"学生的解决方案是正确的。\n",
"\n",
"他们正确地计算了每个部分的费用:\n",
"\n",
"1. **土地费用**:每平方英尺100美元,因此总费用为 \\(100x\\)。\n",
"2. **太阳能电池板费用**:每平方英尺250美元,因此总费用为 \\(250x\\)。\n",
"3. **维护费用**:固定费用为10万美元,加上每平方英尺10美元,因此总费用为 \\(100,000 + 10x\\)。\n",
"\n",
"将所有费用加在一起:\n",
"\n",
"- 土地费用:\\(100x\\)\n",
"- 太阳能电池板费用:\\(250x\\)\n",
"- 维护费用:\\(100,000 + 10x\\)\n",
"\n",
"总费用为:\n",
"\n",
"\\[ 100x + 250x + 100,000 + 10x = 360x + 100,000 \\]\n",
"\n",
"学生的解决方案中有一个小错误,他们将维护费用的每平方英尺费用写成了100x,而不是10x。因此,正确的总费用应该是:\n",
"\n",
"\\[ 360x + 100,000 \\]\n",
"\n",
"而不是学生所写的 \\(450x + 100,000\\)。\n"
]
}
],
Expand Down Expand Up @@ -584,22 +608,34 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"首先计算土地费用:100美元/平方英尺 * x平方英尺 = 100x美元\n",
"然后计算太阳能电池板费用:250美元/平方英尺 * x平方英尺 = 250x美元\n",
"接着计算维护费用:10万美元 + 10美元/平方英尺 * x平方英尺 = 10万 + 10x美元\n",
"最后计算总费用:100x美元 + 250x美元 + 10万美元 + 10x美元 = 360x + 10万美元\n",
"实际解决方案和步骤:\n",
"\n",
"1. **土地费用**:每平方英尺100美元,因此土地费用为 \\(100x\\) 美元。\n",
"\n",
"2. **太阳能电池板费用**:每平方英尺250美元,因此太阳能电池板费用为 \\(250x\\) 美元。\n",
"\n",
"3. **维护费用**:每年固定费用为10万美元,外加每平方英尺10美元,因此维护费用为 \\(100,000 + 10x\\) 美元。\n",
"\n",
"4. **总费用**:将所有费用相加,得到首年运营的总费用:\n",
" \\[\n",
" 100x + 250x + 100,000 + 10x = 360x + 100,000\n",
" \\]\n",
"\n",
"学生计算的总费用:450x + 100,000美元\n",
"\n",
"实际计算的总费用:360x + 100,000美元\n",
"\n",
"学生计算的总费用:450x + 10万美元\n",
"实际计算的总费用:360x + 10万美元\n",
"学生计算的费用和实际计算的费用是否相同:否\n",
"\n",
"学生的解决方案和实际解决方案是否相同:否\n",
"\n",
"学生的成绩:不正确\n"
]
}
Expand Down Expand Up @@ -659,26 +695,32 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1. 论文标题: \"Extrapolating LLM Lengths: A Study on the Impact of Training Data Size\"\n",
"主要内容: 该论文研究了在训练数据规模不断增加的情况下,LLM长度的外推效果。通过实验和数据分析,论文探讨了训练数据规模对LLM长度外推性能的影响,并提出了一些改进方法。\n",
"链接:https://arxiv.org/abs/2106.12345\n",
"以下是一些关于大语言模型(LLM)长度外推的研究论文,包括论文标题、主要内容的简要介绍:\n",
"\n",
"1. **\"Scaling Laws for Neural Language Models\"** \n",
" *主要内容*:这篇论文探讨了语言模型的性能如何随着模型大小、数据集大小和计算量的增加而变化。研究发现,模型的性能遵循某种幂律关系,这为理解和预测更大规模模型的行为提供了理论基础。 \n",
" *链接*:[arXiv](https://arxiv.org/abs/2001.08361)\n",
"\n",
"2. **\"Language Models are Few-Shot Learners\"** \n",
" *主要内容*:该论文介绍了GPT-3,一个具有1750亿参数的语言模型,并展示了其在少样本学习任务中的强大能力。研究中也涉及到模型规模对性能的影响,提供了关于模型扩展的见解。 \n",
" *链接*:[arXiv](https://arxiv.org/abs/2005.14165)\n",
"\n",
"2. 论文标题: \"Analyzing the Extrapolation of LLM Lengths in Natural Language Understanding Tasks\"\n",
"主要内容: 该论文分析了LLM长度在自然语言理解任务中的外推情况。通过实验和对比分析,论文探讨了不同任务对LLM长度外推的需求,以及如何优化LLM模型在不同长度下的性能。\n",
"链接:https://arxiv.org/abs/2110.67890\n",
"3. **\"Training Compute-Optimal Large Language Models\"** \n",
" *主要内容*:这篇论文研究了在给定计算预算下训练大规模语言模型的最优策略。作者提出了一种新的计算预算分配方法,以提高模型的训练效率和性能。 \n",
" *链接*:[arXiv](https://arxiv.org/abs/2203.15556)\n",
"\n",
"3. 论文标题: \"Improving LLM Length Extrapolation through Data Augmentation Techniques\"\n",
"主要内容: 该论文提出了一种通过数据增强技术来改进LLM长度外推的方法。通过在训练数据中引入多样性和噪声,论文探讨了如何提高LLM模型在不同长度下的泛化能力。\n",
"链接:https://arxiv.org/abs/2201.23456\n",
"4. **\"An Empirical Study on Scaling Pre-trained Language Models\"** \n",
" *主要内容*:论文分析了预训练语言模型在不同规模下的表现,研究了模型大小、数据量和计算资源之间的关系,并提供了实证结果以支持理论推断。 \n",
" *链接*:[arXiv](https://arxiv.org/abs/2104.06305)\n",
"\n",
"希望以上论文能够帮助到您的研究工作。\n"
"这些论文提供了关于大语言模型扩展的理论和实证研究,帮助理解如何有效地扩展模型以提高性能。请注意,访问这些链接可能需要科学上网工具。\n"
]
}
],
Expand All @@ -695,15 +737,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"模型给出的论文信息看上去非常正确,但如果打开链接,会发现 404 或者指向的论文不对。也就是说,论文的信息或者链接是模型捏造的。\n",
"模型给出的论文信息看上去非常正确,但如果打开链接,会发现部分链接打开后显示 404 或者指向的论文不对。也就是说,论文的信息或者链接是模型捏造的。\n",
"\n",
"语言模型的幻觉问题事关应用的可靠性与安全性。开发者有必要认识到这一缺陷,并采取 Prompt优化、外部知识等措施予以缓解,以开发出更加可信赖的语言模型应用。这也将是未来语言模型进化的重要方向之一。"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "universe_0_3",
"language": "python",
"name": "python3"
},
Expand All @@ -717,7 +759,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
"version": "3.10.16"
}
},
"nbformat": 4,
Expand Down
Loading