Skip to content

参考官网代码,无法在headers里面加自定义请求头参数 #1042

@lijun-wu

Description

@lijun-wu

重现步骤

在线示例链接:(必填)

步骤:
import { CopyOutlined, ReloadOutlined } from "@ant-design/icons";
import {
Attachments,
Bubble,
Sender,
Welcome,
useXAgent,
useXChat,
XRequest,
} from "@ant-design/x";
import { Button, Flex, type GetProp, Select, Space, Spin, message } from "antd";
import React, { useCallback, useEffect, useRef, useState } from "react";
import styles from "./index.less";
import MarkdownEditor from "@/components/MarkdownEditor";

type BubbleDataType = {
role: string;
content: string;
};

export default () => {
const abortController = useRef(null);
const [inputValue, setInputValue] = useState("生成一篇文章");

// ==================== Runtime ====================
const [agent] = useXAgent({
baseURL:
"https://api.x.ant.design/api/llm_siliconflow_deepSeek-r1-distill-1wen-7b",
model: "DeepSeek-R1-Distill-Qwen-7B",
});
const loading = agent.isRequesting();

const { onRequest, messages, setMessages } = useXChat({
agent,
requestFallback: (_, { error }) => {
if (error?.name === "AbortError") {
return {
content: "请求已中止",
role: "assistant",
};
}
return {
content: "请求失败,请重试!",
role: "assistant",
};
},
transformMessage: (info) => {
const { originMessage, chunk } = info || {};

  let currentContent = "";
  let currentThink = "";
  try {
    if (chunk?.data && !chunk?.data.includes("DONE")) {
      const message = JSON.parse(chunk?.data);
      currentThink = message?.choices?.[0]?.delta?.reasoning_content || "";
      currentContent = message?.choices?.[0]?.delta?.content || "";
    }
  } catch (error) {}

  let content = "";

  if (!originMessage?.content && currentThink) {
    content = `<think>${currentThink}`;
  } else if (
    originMessage?.content?.includes("<think>") &&
    !originMessage?.content.includes("</think>") &&
    currentContent
  ) {
    content = `${originMessage?.content}</think>${currentContent}`;
  } else {
    content = `${originMessage?.content || ""}${currentThink}${currentContent}`;
  }

  return {
    content: content,
    role: "assistant",
  };
},
resolveAbortController: (controller) => {
  abortController.current = controller;
},

});

// ==================== Event ====================
const onSubmit = (val: string) => {
if (!val) return;

if (loading) {
  message.error("请求正在进行中,请等待请求完成。");
  return;
}

onRequest({
  stream: true,
  message: { role: "user", content: val },
  params: {
    sessionId: "current-session-id", // 替换为实际值
    userId: "current-user-id", // 替换为实际值
  },
});

};

// ==================== Nodes ====================
const chatList = (


{messages?.length ? (
/* 🌟 消息列表 */
<Bubble.List
items={messages?.map((i) => {
return {
...i.message,
content: ,
classNames: {
content: i.status === "loading" ? styles.loadingMessage : "",
},
typing:
i.status === "loading"
? { step: 5, interval: 20, suffix: <>💗</> }
: false,
};
})}
style={{
height: "100%",
paddingInline: "calc(calc(100% - 700px) /2)",
}}
roles={{
assistant: {
placement: "start",
footer: (
<div style={{ display: "flex" }}>
<Button type="text" size="small" icon={} />
<Button type="text" size="small" icon={} />

),
loadingRender: () => ,
},
user: { placement: "end" },
}}
/>
) : (
<Space
direction="vertical"
size={16}
style={{ paddingInline: "calc(calc(100% - 700px) /2)" }}
className={styles.placeholder}
>


)}

);

const chatSender = (
<>
{/* 🌟 输入框 */}
<Sender
value={inputValue}
onSubmit={() => {
onSubmit();
setInputValue("");
}}
onChange={setInputValue}
onCancel={() => {
if (
abortController.current &&
!abortController.current.signal.aborted
) {
abortController.current.abort("用户取消");
}
}}
loading={loading}
className={styles.sender}
allowSpeech
footer={({ components }) => {
const { SendButton, LoadingButton, ClearButton } = components;
return (


{loading ? ( ) : ( )} ); }} actions={false} placeholder="请输入..." /> </> ); // ==================== Render ================= return ( {chatList} {chatSender} ); }; 当前行为 我看官网是说要用XRequest这个方法,但是这个方法怎么用啊?如何与我的数据连接起来 预期行为 No response 上下文 No response 版本 1.5.0 您在哪些浏览器上遇到了这个问题? No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions