首页 > 其他分享 >使用Code-Prompt模拟实现openai o1: V2

使用Code-Prompt模拟实现openai o1: V2

时间:2024-09-20 20:24:09浏览次数:10  
标签:Code Prompt expert step analysis expt V2 result query

在之前的一个版本中, 展现出来一定的思考能力, 但是这只是一种表演型的思考能力, 为什么?

根据实际的观察, 我认为是因为规划的部分, 这部分实际上是有问题的, 将后续的Step限制的太严重了.

改进思路

其实在上一期已经提过了, 那就是怎么思考, 思考不是能够在一开始就就能够规划好的

那么问题来了, 怎么做呢?

  1. 在最开始的时候, 分析: 领域/问题/限制条件

  2. Act Expert: 扮演专家重新分析问题

  3. Loop: Step, 从分析触发, 每一步的输入参数包括

    • 原始问题
    • 已经采取的步骤
    • 上一个步骤后得到的结果

    输出参数应该包括:

    • 本步骤要做的事情, 很小的一步
    • 概括本步骤做的事情
    • 本步骤所解决的问题
    • 本步骤得到的结果
  4. 最后的总结

Prompt

# YOU ARE A PROCESS, EXECUTE THE FOLLOWING CODE!
# ONLY OUTPUT THE CODE RESULT!

# llm Package is yourself(LLM)'s ability
from llm import (
    deepthink,
    expert,
    infer,
    judge,
)
from llm.io import (
    input,
    output,
)


def analyze_query(query: str):
    res = {}
    # 1. understand the query
    understandings = deepthink(f"what is the most important definition or concept of '{query}'?", list_length=(2, 10))
    res["key_word"] = understandings

    # 2. knowledge regions
    knowledge_region = deepthink(f"what knowledge regions does query: '{query}' belong to?", list_length=(3, 20))
    res["knowledge_region"] = knowledge_region

    return res


def get_expert(regions, key_words):
    # target: get an expert based on the knowledge region
    expt = expert(regions=regions, key_words=key_words)
    return expt


def expert_analyze(q: str, expert):
    # target: analyze the query in the expert's view
    # e.g. "what is the most important part of the query?"
    # e.g. "what is the most important concept/definition/theorem/formula in the query?"
    # e.g. "what should i take care of when solve the query?"
    # DO NOT take action, just analyze the query
    res = expert.analyze(q, structure_output=True)
    return res


def get_final_result(result: dict):
    # target: give the final result of the query
    final_result = infer(
        raw_query=result["raw_query"],
        analysis=result["expert_analysis"],
        actions=result["actions"])  # infer the final result from the raw query, query analysis and actions
    return final_result


def step(q: str, expert, prev_steps):
    # target: do the next little step

    # 0. check if the query is solved
    if judge(query=q, judge_by="is the query solved?"):
        return None

    # 1. what to do, just a slow, clear and simple action
    todo = expert.plan(f"if i want to solve the query: '{q}', what should i do? i have done the following steps: {prev_steps}")

    # do the action
    # e.g. if in math, do a simple math calculation or formula or theorem or definition or concept
    # e.g. if in programming, do analysis/design/coding/testing/debugging
    # e.g. if in language, do translation/grammar/word meaning/word usage
    # e.g. if in history, do analysis of historical events or historical figures
    action = expert.do(todo)
    # 2. check the result
    # check the action result in the expert's view, like
    #   "is the result correct?"
    #   "is the result reasonable?"
    #   "is the result acceptable?"
    #   "is the result good enough?"
    # give the detailed reason why the result is correct or not
    check = expert.check(action).result
    # 3. Periodic summary of current result of the query
    # summary = expert.summary(q, action, check, prev_steps)

    step_result = {
        "action": action,
        "check": check,
        # "summary": summary
    }
    return step_result


def o1(query: str):

    result = {"raw_query": query}
    # step 1: analyze the query
    analysis = analyze_query(query)
    result["analysis"] = analysis

    # step 2: sub-questions
    expt = get_expert(analysis["knowledge_region"], analysis["key_word"])
    result["expert"] = {
        "name": expt.name,
        "description": expt.description  # expert's description. e.g. "I am an expert in ..."
    }
    expt_query = deepthink(f"Rephrase the question: {query} using professional language.", expert=expt)
    result["expert_query"] = expt_query
    # step 3: schedule a plan to solve the query
    result["expert_analysis"] = expert_analyze(expt_query, expt)
    # step 4: action steps
    steps = []
    q = query
    while True:
        step_result = step(q, expt, steps)
        if step_result is None:
            break
        steps.append(step_result)
    result["actions"] = steps

    # step 5: give the result
    final_answer = get_final_result(result)
    result["final_answer"] = final_answer

    return result


# main function
if __name__ == '__main__':
    query = input("Ask me any question, i will tell you the answer:")
    result = o1(query)
    # result = analyze(query)
    output(result, output_format="json", code_region=True)  # output the result in json format but in a code region:‍```json ... ‍```

结论

有一点效果, 但是效果还是很垃圾

  1. 自动的CoT是不行的, 起码自动生成还是不好的, 可能还是需要一个CoT模板
  2. 需要一个反省​的逻辑来处理, 可能会好一些

标签:Code,Prompt,expert,step,analysis,expt,V2,result,query
From: https://www.cnblogs.com/pDJJq/p/18423220/use-codeprompt-to-simulate-openai-o1-v2-1cjpfx

相关文章

  • 华为CodeArts测评
    华为CodeArts(原华为代码管理平台,现升级为华为云CodeArts)是华为云推出的一款代码托管和协同开发工具,主要针对企业和开发者提供一站式代码管理、版本控制、代码审查、持续集成/持续部署(CI/CD)、项目管理等功能。以下是CodeArts的主要特点和功能:全栈服务:覆盖代码创建、开发、测试、部署......
  • ANTLR Tool version 4.13.1 used for code generation does not match the current ru
    ANTLRToolversion4.13.1usedforcodegenerationdoesnotmatchthecurrentruntimeversion4.7.2当我使用neo4j过程中,遇到了该问题:ANTLRToolversion4.13.1usedforcodegenerationdoesnotmatchthecurrentruntimeversion4.7.2ANTLRRuntimeversion4.13.1......
  • 我在 Marscode 用了 3 天,转行成为 Python 程序员
    以下是「 豆包MarsCode 体验官」优秀文章,作者不惑_。豆包MarsCode 项目实战Java程序员转行Python学习之路俗话说:工欲善其事,必先利其器。在历史的长河中,新手程序员最大的痛点之一就是搭建开发环境。先就是今天,如果你没有VSCode,甚至也没有其他IDE,那么也没有关系。豆包......
  • 闯关leetcode——58. Length of Last Word
    大纲题目地址内容解题代码地址题目地址https://leetcode.com/problems/length-of-last-word/description/内容Givenastringsconsistingofwordsandspaces,returnthelengthofthelastwordinthestring.Awordisamaximalsubstringconsisting......
  • Educational Codeforces Round 135 (Rated for Div. 2)D. Letter Picking
    注意读题,每次拿完之后是放在开头。所以先手不败,因为最后剩下两个的时候,先手一定可以取较小值。考虑怎样会出现平局?首先已经知道了先手不败,那么对于后手来说,他追求的就是平局,也就是尽可能的保证每一步都都与先手相同。所以,如果是回文串,或者两两相同,或者回文串包两两相同的情况,才......
  • ChatGPT提示词(Prompt)框架
    ChatGPT提示词(Prompt)框架    构建有效的ChatGPT提示词(Prompt)框架的指南,我们提供了几个不同的框架示例,每个框架都旨在帮助用户更精确地指导ChatGPT以完成特定的任务:详细解读:ChatGPT提示词框架解锁ChatGPT的全部潜力R-T-F(Role-Task-Format)PromptExample(示例提示):角色(Role):......
  • VSCode 定义Java类注释
    在使用VSCode开发Java时,输入/**生成的类注释如下:/****/这样的注释一片空白,无法标注类的作者、创建时间等信息。可以通过如下设置实现更贴合Java类的注释:进入Java配置输入settings.json​ 选择Preferences:OpenUserSettings(JSON)添加Java配......
  • VSCode 定义代码模板
    在使用编写代码的过程中,经常会写一些固定代码段。以Java为例,定义实体类时一般都会编写序列化版本号:@SerialprivatestaticfinallongserialVersionUID=1L;这段代码是固定写法,基本不会变,如果每次都手写的话,就比较繁琐了。VSCode提供了生成代码段的功能,为我们提供了便......
  • Educational Codeforces Round 136 (Rated for Div. 2) D. Reset K Edges
    这道题目我们可以考虑二分做,二分出最终的深度,然后尝试是否能使用不超过\(k\)次操作使得深度符合条件。考虑如何和判断,我们可以从根节点开始搜索,如果当前点的深度为\(mid+1\),就对当前点进行操作。但很可惜,这种贪心方法可以很容易的举出反例,比如深度为\(mid\)的点下面有很多个叶......
  • VSCode配置STM32HAL库开发环境
    1.开发工具下载下载STM32CubeMX:https://www.st.com.cn/zh/development-tools/stm32cubemx.html(可通过访客方式下载)安装时一直默认即可下载stm32固件库:在线下载方式(需要登陆):安装好STM32CubeMX后,在菜单栏【Help】->【ManageEmbeddedSoftwarePackages】里选择合适的固......