如何通过API抽取数据并存入数据库

通过 axios 调用API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const abcApi = env.CONTEST_API_URL;
const abcApiToken = env.CONTEST_API_TOKEN;

console.log(`${abcApi} start`);
const { data } = await axios.get(`${abcApi}/` , {
headers: {
Accept: 'application/json',
Authorization: `ApiKey ${abcApiToken}`
},
params: {
resource_id: 93,
limit: 700,
order_by: '-end',
},
});
return data.objects as ContestAPIType[];

其中 ContestAPIType 可以根据返回值定义。

More...

从零搭建基于 AI GitHub 分析平台 (17) 保存答案

添加 saveAnswer 函数

修改 src/server/api/routers/project.ts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
saveAnswer: authenticatedProcedure.input( z.object({
projectId: z.string(),
question: z.string(),
answer: z.string(),
fileReferences: z.any(),
})).mutation( async ( { ctx, input }) => {
return await ctx.db.question.create({
data: {
answer: input.answer,
fileReferences: input.fileReferences,
projectId: input.projectId,
question: input.question,
userId: ctx.user.userId!,
}
})
})

修改 AskQuestionCard 页面

修改 `src/app/(protected)/dashboard/AskQuestionCard.tsx’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { api } from '@/trpc/react';
import { toast } from 'sonner';
...
const [ fileReferences, setFileReferences ] = useState<{ fileName :string, sourceCode: string, summary:string }[]>([])
const saveAnswer = api.project.saveAnswer.useMutation();
...
const onSubmit = async ( e: React.FormEvent<HTMLFormElement>) => {
setAnswer("");
setFileReferences([]);
e.preventDefault();
if(!project?.id) return;
setLoading(true);

const {output , fileReferences} = await askQuestion(question,project.id);
setFileReferences(fileReferences);
setAnswer(output);

}
...
<div className='flex items-center gap-2'>
<DialogTitle>
<Image src='/logo.png' alt='aigithub' width={40} height={40} />
</DialogTitle>
<Button disabled={saveAnswer.isPending} variant={'outline'} onClick={()=>{
saveAnswer.mutate({
projectId: project!.id,
question,
answer,
fileReferences
}, {
onSuccess: ()=>{
toast.success('答案保存成功')
},
onError: () => {
toast.error('答案保存错误');
}
})
}}>
保存答案
</Button>
</div>

作者:Bearalise
出处:从零搭建基于 AI GitHub 分析平台 (17) 保存答案
版权:本文版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明,必须在文章中给出原文链接。

如何刷新 VS Code 中 Prisma 更新的表和字段

背景

当我更新 Prisma Schema 的时候,有时会出现即使通过 prisma db pushprisma generate 更新了数据库,也显示成功,但在 VS Code 中依然无法通过代码自动完成看到更新的字段,比较困扰,有时重新打开 VS Code 会好,有时不会

解决方案

原因主要是 VS Code 的缓存没有及时更新,查询后发现一个立即更新缓存的方法,就是在 VS Code 中,找到 node_modules/.prisma/client/index.d.ts ,并打开,这时 VS Code 就会自动更新缓存。我测试了一下有效,大家可以试试。

More...

PostgreSQL 带向量数据库的安装

背景

做 AI 相关项目需要用到向量数据库,本来想直接使用 PostgreSQL ,结果发现默认的安装是没有安装向量数据库的,打算重新安装一个带向量支持的。

实现步骤如下:

Create Volume

创建一个相关的卷用于保存数据

1
docker volume create --driver local --opt device=C:\10.VM\data\pg02 --opt type=none --opt o=bind pg02

拉取相关 Docker 镜像

下面这个镜像是包含向量数据库的:

1
docker pull ankane/pgvector:latest

启动 docker

1
docker run --name pgvector --privileged -e POSTGRES_PASSWORD=pGp@ssw0rd -p 15678:5432 -v pg02:/var/lib/postgresql/data -d ankane/pgvector:latest
More...

从零搭建基于 AI GitHub 分析平台 (12) 批量生成总结并存储数据库

调整数据库,添加相关表

修改 prisma/schema.prisma:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
model Project {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
githubURL String
deleteAt DateTime?
usersProject UsersProject[]
commits Commit[]
}

model Commit {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

projectId String
project Project @relation(fields: [projectId], references: [id])

commitMessage String
commitHash String
commitAuthorName String
commitAuthorAvatar String
commitDate DateTime
// ai summary
summary String
}
More...

从零搭建基于 AI GitHub 分析平台 (11) 基于commit diff 生成 commit 的总结

效果:

执行语句 bun run src/lib/gpt.ts :

1
2
3
4
5
6
7
8
- Set `randomOption` default value to `1` in the examination page.
- Add `zod` schema validation and new hooks for exam records and questions.
- Replace `useStartWCDialog` with `useExamDialog` for handling exam dialogs.
- Add logic to handle wrong question collections and create new exam records.
- Include `questionNum` in the API response for questions of a repository.
- Replace `thumbUrl` with `isWrongCollection` in the repositories API response.
- Reorganize imports and adjust button layout in the new exam dialog component.
- Filter repositories to hide items with `isWrongCollection` set to `1`.

我拿了一个比较复杂的代码提交,总结的比较到位。

More...

请我喝杯咖啡吧~

支付宝
微信