forked from Gitlink/gitlink_help_center
This commit is contained in:
parent
562d62f771
commit
d8aa80fa0b
34
answer.py
34
answer.py
|
@ -1,34 +0,0 @@
|
|||
import os
|
||||
import json
|
||||
import requests
|
||||
|
||||
from pdfminer.high_level import extract_text
|
||||
import re
|
||||
|
||||
def send_pdf_and_get_response(message):
|
||||
# Moonshot API 密钥
|
||||
MOONSHOT_API_KEY = "fea58b082997a69f199264ce8221430a.zC0hu8lce49CNBPJ"
|
||||
|
||||
# 构造请求的 headers
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': f'Bearer {MOONSHOT_API_KEY}',
|
||||
}
|
||||
# 构造请求数据
|
||||
data = {
|
||||
"model": "glm-4", # Moonshot 模型
|
||||
"messages": [
|
||||
{"role": "user", "content":message}
|
||||
],
|
||||
"temperature": 0.01, # 温度参数,控制助手回复的多样性
|
||||
}
|
||||
|
||||
# 发送 POST 请求
|
||||
response = requests.post('https://open.bigmodel.cn/api/paas/v4/chat/completions', headers=headers, data=json.dumps(data))
|
||||
response_json = response.json()
|
||||
|
||||
# 提取助手的回复
|
||||
assistant_response = response_json['choices'][0]['message']['content']
|
||||
|
||||
return assistant_response
|
||||
print(send_pdf_and_get_response(1))
|
|
@ -4,7 +4,7 @@ sidebar_position: 1
|
|||
---
|
||||
|
||||
import { MDXCreateElement } from '@mdx-js/react';
|
||||
import MyForm from '../../src/pages/MyForm';
|
||||
import YourComponent from '../../src/pages/MyForm';
|
||||
|
||||
# 常见问题
|
||||
|
||||
|
@ -25,4 +25,4 @@ import MyForm from '../../src/pages/MyForm';
|
|||
|
||||
## 提出你的问题
|
||||
|
||||
<MyForm />
|
||||
<YourComponent />
|
||||
|
|
|
@ -1,26 +1,34 @@
|
|||
// MyForm.js
|
||||
import React, { useRef } from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import axios from 'axios';
|
||||
|
||||
const MyForm = () => {
|
||||
const inputRef = useRef(null);
|
||||
const YourComponent = () => {
|
||||
const [message, setMessage] = useState('');
|
||||
const [assistantResponse, setAssistantResponse] = useState('');
|
||||
|
||||
const handleSubmit = (event) => {
|
||||
const handleSubmit = async (event) => {
|
||||
event.preventDefault();
|
||||
const inputValue = event.target.inputText.value;
|
||||
alert('AI智能回答:' + inputValue);
|
||||
|
||||
// 清空输入框的值
|
||||
inputRef.current.value = '';
|
||||
|
||||
// 在这里可以添加向后端发送数据的逻辑
|
||||
try {
|
||||
const response = await axios.post('http://localhost:5000/moonshot', { message });
|
||||
setAssistantResponse(response.data.assistantResponse);
|
||||
} catch (error) {
|
||||
console.error('Error communicating with backend:', error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<form id="myForm" onSubmit={handleSubmit}>
|
||||
<input type="text" id="inputText" placeholder="提出你的问题..." name="inputText" ref={inputRef} />
|
||||
<input type="submit" value="提交" />
|
||||
</form>
|
||||
<div>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<input type="text" value={message} onChange={(e) => setMessage(e.target.value)} />
|
||||
<button type="submit">提交</button>
|
||||
</form>
|
||||
<div>
|
||||
AI回复: {assistantResponse}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default MyForm;
|
||||
|
||||
export default YourComponent;
|
||||
|
||||
|
|
Loading…
Reference in New Issue