Agent techniques to augment your LLM and push it beyong its limits
1.6k
Stars
135
Forks
24
Watchers
16
Issues
ThinkGPT 是一个 Python 库,旨在为大语言模型(LLM)实现思维链(Chain of Thoughts),引导模型进行思考、推理,并创建生成式 Agent。 该库旨在解决以下问题:
你可以使用 pip 安装 ThinkGPT:
pip install git+https://github.com/alaeddine-13/thinkgpt.git
from thinkgpt.llm import ThinkGPT
llm = ThinkGPT(model_name="gpt-3.5-turbo")
# 让 llm 对象学习新概念
llm.memorize(['DocArray is a library for representing, sending and storing multi-modal data.'])
llm.predict('what is DocArray ?', remember=llm.remember('DocArray definition'))
llm.memorize([
'DocArray allows you to send your data, in an ML-native way.',
'This means there is native support for Protobuf and gRPC, on top of HTTP and serialization to JSON, JSONSchema, Base64, and Bytes.',
])
print(llm.remember('Sending data with DocArray', limit=1))
['DocArray allows you to send your data, in an ML-native way.']
使用 limit 参数可以指定检索的最大文档数。
如果你希望将文档适配到特定的上下文大小,也可以使用 max_tokens 参数来指定检索的最大 token 数。
例如:
from examples.knowledge_base import knowledge
from thinkgpt.helper import get_n_tokens
llm.memorize(knowledge)
results = llm.remember('hello', max_tokens=1000, limit=1000)
print(get_n_tokens(''.join(results)))
1000
需要注意的是,使用分隔符连接文档会增加最终结果的 token 数量。remember 方法不会计算这些连接符产生的 token。
from examples.knowledge_base import knowledge
llm.memorize(knowledge)
llm.predict('Implement a DocArray schema with 2 fields: image and TorchTensor', remember=llm.remember('DocArray schemas and types'))
print(llm.refine(
content="""
import re
print('hello world')
""",
critics=[
'File "/Users/user/PyCharm2022.3/scratches/scratch_166.py", line 2',
" print('hello world')",
'IndentationError: unexpected indent'
],
instruction_hint="Fix the code snippet based on the error provided. Only provide the fixed code snippet between `` and nothing else."))
import re
print('hello world')
其应用场景之一是 gptdeploy 和 wolverine 等项目实现的自愈式代码生成。
如果你希望知识适配 LLM 的上下文,可以使用以下技术进行压缩:
使用 LLM 本身来总结内容。 我们提供两种方法:
llm.summarize(
large_content,
max_tokens= 1000,
instruction_hint= 'Pay attention to code snippets, links and scientific terms.'
)
由于该技术依赖于单次 LLM 调用进行总结,因此只能处理不超过 LLM 上下文长度的内容。
llm.chunked_summarize(
very_large_content,
max_tokens= 4096,
instruction_hint= 'Pay attention to code snippets, links and scientific terms.'
)
该技术通过将内容分割成不同的块,对每个块进行摘要,最后由 LLM 将它们汇总在一起。
从当前的观察结果中提升到更高层次、更通用的概括:
llm.abstract(observations=[
"in tunisian, I did not eat is \"ma khditech\"",
"I did not work is \"ma khdemtech\"",
"I did not go is \"ma mchitech\"",
])
['Negation in Tunisian Arabic uses "ma" + verb + "tech" where "ma" means "not" and "tech" at the end indicates the negation in the past tense.']
这可以帮助你得到压缩后的知识,从而使其符合上下文长度限制。
ThinkGPT 是一个基于 Python 的开源库,旨在通过实现思维链(Chain of Thought)技术来扩展大语言模型的能力。它通过提供高级推理原语,帮助开发者突破模型上下文长度限制,并使大模型具备持续记忆与智能决策能力。
该库提供记忆模块,使 GPT 能够存储并调用过往经验。支持自我优化机制,通过反馈机制提升模型生成内容的质量。具备知识压缩功能,能够通过摘要或规则抽象将大量信息适配到有限的上下文窗口中。支持自然语言条件逻辑,简化了复杂判断流程的编码实现。提供简洁的 Python 风格 API,可快速集成到现有代码库中。
适用于希望提升大模型推理深度与记忆能力的开发者,广泛应用于智能自动化代理、自我修正代码生成及长文档复杂任务处理等场景。