{"id":25463324,"url":"https://github.com/xiaomingx/groq-o1-example","last_synced_at":"2025-06-30T03:06:16.341Z","repository":{"id":263242133,"uuid":"889775184","full_name":"XiaomingX/groq-o1-example","owner":"XiaomingX","description":"This repository demonstrates using the Groq O1 for optimized machine learning and data processing. Learn to leverage Groq’s power for accelerated execution.","archived":false,"fork":false,"pushed_at":"2025-02-01T14:42:19.000Z","size":27,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T06:51:14.715Z","etag":null,"topics":["api","example","groq","inference","openai"],"latest_commit_sha":null,"homepage":"https://twitter.com/seclink","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/XiaomingX.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-17T07:39:53.000Z","updated_at":"2025-02-08T18:48:46.000Z","dependencies_parsed_at":"2025-02-18T06:40:57.648Z","dependency_job_id":null,"html_url":"https://github.com/XiaomingX/groq-o1-example","commit_stats":null,"previous_names":["xiaomingx/groq-o1-example"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/XiaomingX/groq-o1-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiaomingX%2Fgroq-o1-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiaomingX%2Fgroq-o1-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiaomingX%2Fgroq-o1-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiaomingX%2Fgroq-o1-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/XiaomingX","download_url":"https://codeload.github.com/XiaomingX/groq-o1-example/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XiaomingX%2Fgroq-o1-example/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262702325,"owners_count":23350641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","example","groq","inference","openai"],"created_at":"2025-02-18T06:29:39.679Z","updated_at":"2025-06-30T03:06:16.298Z","avatar_url":"https://github.com/XiaomingX.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"本文将带你一步步实现一个基于 **Groq API** 的推理助手，该助手能够高效地模拟接近chatgpt-o1的推理逻辑。通过该教程，你将掌握以下关键知识点：\n\n## 一、Groq API 基础\n\n### 1.1 安装依赖\n\n确保你的环境中安装了以下库：\n\n```bash\npip install groq-python dotenv\n```\n\n还需要确保你已经获取了 Groq API 的密钥，并将其保存在 `.env` 文件中：\n\n```env\nGROQ_API_KEY=your_api_key_here\n```\n\n### 1.2 初始化客户端\n\n通过 `dotenv` 加载环境变量，创建一个 Groq API 客户端：\n\n```python\nimport groq\nfrom dotenv import load_dotenv\nload_dotenv()\n\nclass GroqAPIClient:\n    def __init__(self, custom_client=None):\n        self.client = custom_client or groq.Groq()\n```\n\n***\n\n## 二、实现等同于chatgpt-o1的推理模型\n\n### 2.1 构建推理助手类\n\n**核心逻辑**：推理助手通过 API 循环调用模拟逐步推理的过程，每一步都包含以下内容：\n\n*   当前步骤的标题（`title`）\n*   推理内容（`content`）\n*   是否需要进入下一步或返回最终答案（`next_action`）\n\n#### 推理助手核心代码\n\n```python\nclass ReasoningAssistant:\n    def __init__(self, api_client):\n        self.api_client = api_client\n\n    def generate_response(self, prompt):\n        messages = self._initialize_messages(prompt)\n        steps = []\n        step_count = 1\n        total_thinking_time = 0\n\n        while True:\n            start_time = time.time()\n            step_data = self.api_client.make_api_call(messages, 300)\n            end_time = time.time()\n            thinking_time = end_time - start_time\n            total_thinking_time += thinking_time\n\n            steps.append((f\"Step {step_count}: {step_data['title']}\", step_data['content'], thinking_time))\n            messages.append({\"role\": \"assistant\", \"content\": json.dumps(step_data)})\n\n            if step_data['next_action'] == 'final_answer' or step_count \u003e 25:  # 防止死循环\n                break\n\n            step_count += 1\n            yield steps, None\n\n        final_answer = self._generate_final_answer(messages)\n        steps.append((\"Final Answer\", final_answer['content'], final_answer['thinking_time']))\n        yield steps, total_thinking_time + final_answer['thinking_time']\n\n        self._save_to_markdown(prompt, steps)\n```\n\n***\n\n### 2.2 API 调用及容错机制\n\n通过 `make_api_call` 方法封装 API 调用逻辑，并添加了重试机制：\n\n```python\nclass GroqAPIClient:\n    def make_api_call(self, messages, max_tokens, is_final_answer=False):\n        for attempt in range(3):  # 最多尝试三次\n            try:\n                response = self.client.chat.completions.create(\n                    model=\"llama-3.1-70b-versatile\",\n                    messages=messages,\n                    max_tokens=max_tokens,\n                    temperature=0.2,\n                    response_format={\"type\": \"json_object\"} if not is_final_answer else None\n                )\n                if is_final_answer:\n                    return response.choices[0].message.content\n                else:\n                    return json.loads(response.choices[0].message.content)\n            except Exception as e:\n                if attempt == 2:\n                    error_content = {\n                        \"title\": \"Error\",\n                        \"content\": f\"Failed after 3 attempts. Error: {str(e)}\"\n                    }\n                    return error_content\n                time.sleep(1)  # 重试前等待 1 秒\n```\n\n***\n\n### 2.3 设计消息格式\n\n推理过程中消息的格式如下：\n\n1.  **系统消息**：定义模型行为和推理规则。\n2.  **用户消息**：传递用户的输入。\n3.  **助手消息**：记录每步推理的结果。\n\n初始化消息的方法如下：\n\n```python\ndef _initialize_messages(self, prompt):\n    return [\n        {\"role\": \"system\", \"content\": self._system_message()},\n        {\"role\": \"user\", \"content\": prompt},\n        {\"role\": \"assistant\", \"content\": \"Thank you! I will now think step by step following my instructions, starting at the beginning after decomposing the problem.\"}\n    ]\n```\n\n系统消息的定义：\n\n```python\ndef _system_message(self):\n    return (\n        \"\"\"\n        You are an expert AI assistant that explains your reasoning step by step...\n        \"\"\"\n    )\n```\n\n***\n\n### 2.4 保存推理过程\n\n将推理过程以 Markdown 文件的形式保存：\n\n```python\ndef _save_to_markdown(self, prompt, steps):\n    filename = f\"{prompt[:20]}.md\".replace(\" \", \"_\").replace(\"/\", \"-\")\n    with open(filename, \"w\", encoding=\"utf-8\") as file:\n        file.write(f\"# Problem: {prompt}\\n\\n\")\n        for step in steps:\n            file.write(f\"## {step[0]}\\n\\n{step[1]}\\n\\n(Thinking time: {step[2]:.2f} seconds)\\n\\n\")\n```\n\n***\n\n## 三、运行推理模型\n\n编写 `main` 函数，启动推理过程：\n\n```python\ndef main():\n    custom_client = None  # 替换为实际的 Groq 客户端\n    api_client = GroqAPIClient(custom_client)\n    assistant = ReasoningAssistant(api_client)\n\n    prompt = \"I am playing Werewolf and I am the Hunter. It is the second round...\"\n    for steps, total_time in assistant.generate_response(prompt):\n        for step in steps:\n            print(f\"{step[0]}:\\n{step[1]}\\n(Thinking time: {step[2]:.2f} seconds)\\n\")\n        if total_time is not None:\n            print(f\"Total Thinking Time: {total_time:.2f} seconds\")\n\nif __name__ == \"__main__\":\n    main()\n```\n\n***\n\n## 四、教程总结\n\n1.  **模块化设计**：将 API 调用、推理逻辑和结果存储分离，增强代码的可维护性。\n2.  **容错机制**：通过重试机制提高调用的可靠性。\n3.  **结果可视化**：将推理过程保存为 Markdown 文件，方便分析和调试。\n\n按照此教程构建的推理模型不仅高效，还具有强大的可扩展性，可应用于复杂推理场景。\n\n# 使用示例-1（做高考和大学的数学题、化学题、物理题）\n\n# Problem: Given the parabola ( y^2 = 16x ), the coordinates of the focus are \\_\\_\\_\\_\\_\\_\\_\\_.\n\n![image.png](https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/711b62fa962a4443871d1e1635a8d54b~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgdWhha2Fkb3Rjb20=:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMjg3NTk3ODE0NzY5MjkxMCJ9\u0026rk3s=f64ab15b\u0026x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018\u0026x-orig-expires=1732434264\u0026x-orig-sign=UfcqiR48U%2Fs3eWKFHTjOhc9ReIM%3D)\n\n## Step 1: Decompose the Problem\n\nThe given equation is ( y^2 = 16x ), which is a parabola in the standard form ( y^2 = 4px ). The goal is to find the coordinates of the focus. To do this, we need to identify the value of ( p ) and use it to determine the coordinates of the focus.\n\n(Thinking time: 1.35 seconds)\n\n## Step 2: Identify the Value of p\n\nComparing the given equation ( y^2 = 16x ) to the standard form ( y^2 = 4px ), we can see that ( 4p = 16 ). Solving for ( p ), we get ( p = 16/4 = 4 ). Therefore, the value of ( p ) is 4.\n\n(Thinking time: 0.73 seconds)\n\n## Step 3: Determine the Coordinates of the Focus\n\nThe coordinates of the focus of a parabola in the standard form ( y^2 = 4px ) are ( p, 0 ). Since we found that ( p = 4 ), the coordinates of the focus are ( 4, 0 ). However, we should also consider alternative approaches to verify our answer.\n\n(Thinking time: 0.79 seconds)\n\n## Step 4: Alternative Approach Using the Definition of a Parabola\n\nA parabola is defined as the set of all points that are equidistant to the focus and the directrix. The directrix of a parabola in the standard form ( y^2 = 4px ) is the line ( x = -p ). Since we found that ( p = 4 ), the directrix is the line ( x = -4 ). Using this information, we can verify that the coordinates of the focus are indeed ( 4, 0 ).\n\n(Thinking time: 1.66 seconds)\n\n## Step 5: Alternative Approach Using the Equation of the Parabola\n\nWe can also use the equation of the parabola to find the coordinates of the focus. The equation ( y^2 = 16x ) can be rewritten as ( x = y^2/16 ). This equation represents a parabola that opens to the right, and the focus is located at a distance ( p ) from the vertex. Since we found that ( p = 4 ), the coordinates of the focus are indeed ( 4, 0 ).\n\n(Thinking time: 1.61 seconds)\n\n## Step 6: Re-examine the Answer Using a Different Method\n\nLet's re-examine our answer by using the formula for the focal length of a parabola, which is given by ( c = 1/(4a) ) for a parabola in the form ( y^2 = 4ax ). Comparing this to our equation ( y^2 = 16x ), we can see that ( 4a = 16 ), so ( a = 4 ). Plugging this into the formula, we get ( c = 1/(4\\*4) = 1/16 ), but this is not what we are looking for. However, we can see that ( c = p = 4 ) is actually the focal length, not the focal coordinate. The focal coordinate is ( p, 0 ) = ( 4, 0 ).\n\n(Thinking time: 1.33 seconds)\n\n## Final Answer\n\nThe coordinates of the focus are (4, 0).\n\n(Thinking time: 0.61 seconds)\n\n# 使用示例-2（ 玩狼人杀或者天黑请闭眼，进行逻辑推理 ）\n\n - 问题描述：我正在玩狼人杀，我是猎人。现在是第二轮，预言家已经死了。在我发言之前，有人宣称自己是猎人，但实际上不是。轮到我发言时，我应该如何应对？\n\n## 第一步：理解问题\n\n当前的情况是，我是猎人，但在我之前有人虚假宣称自己是猎人。而且预言家已经阵亡，当前是第二轮。我需要找到最佳的应对策略，决定在自己发言时该怎么做。\n\n## 第二步：分析局势\n\n由于预言家已死，我作为猎人无法得知谁是狼人。虚假的猎人宣称者可能是狼人，企图误导村民；也可能是普通村民，为了保护自己而撒谎。我需要判断对方的动机，考虑怎样应对才能最小化暴露自己的身份。\n\n## 第三步：可能的应对方式\n\n猎人有几种选择：1）揭穿对方并表明自己才是真正的猎人，2）顺着对方的话，假装相信，或者3）保持沉默，观察其他玩家的反应。每种选择都有利弊：揭穿对方可能会让狼人知道我的身份，而顺着对方的话则可以获得更多关于其动机的信息。\n\n## 第四步：评估暴露身份的风险\n\n如果我暴露了猎人的身份，下一轮可能会成为狼人的攻击目标。但如果保持沉默，可能会错失从虚假猎人身上获得有用信息的机会。我需要权衡暴露身份的风险与可能获得的信息之间的利弊。\n\n## 第五步：考虑其他视角\n\n假设虚假的猎人是一个为了自保的普通村民，或者是一个试图制造混乱的狼人，甚至可能是另一个猎人的同伴（如果游戏中有多个猎人）。我需要从不同的角度考虑，才能更好地判断对方的真实意图。\n\n## 第六步：换个思路重新审视问题\n\n与其专注于揭穿虚假的猎人，不如考虑猎人的主要目标是找出狼人。我可以利用当前的机会，通过提问来获取关于其他玩家的信息，而不直接回应虚假的猎人宣称。这样可以帮助我更好地了解当前的游戏局势，为后续行动做好准备。\n\n## 第七步：综合信息得出结论\n\n经过对局势、应对方式、风险以及不同视角的分析，我认为猎人应该采取一种既能获取信息又能降低风险的策略。具体来说，可以在发言时提出问题，试图了解其他玩家的行为，而不直接揭穿虚假的猎人宣称。这样既能隐藏自己的身份，又能收集更多关于游戏状态的信息。\n\n## 最终结论\n\n轮到你发言时，建议你通过提问来获取其他玩家的信息，而不直接回应虚假的猎人宣称。例如，你可以让大家讨论前一晚的情况，或者询问某些玩家的行为动机。通过这样做，你可以观察其他人的反应，收集有用的信息，为后续决策提供依据，同时尽量隐藏你猎人的真实身份。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiaomingx%2Fgroq-o1-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxiaomingx%2Fgroq-o1-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiaomingx%2Fgroq-o1-example/lists"}