Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
agentchat
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李伟@五瓣科技
agentchat
Commits
a1665fde
Commit
a1665fde
authored
May 30, 2025
by
Wade
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
qa chat
parent
b9d1f783
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
141 additions
and
127 deletions
+141
-127
main.go
main.go
+13
-7
qa.go
qa.go
+128
-120
No files found.
main.go
View file @
a1665fde
...
@@ -2,6 +2,7 @@ package main
...
@@ -2,6 +2,7 @@ package main
import
(
import
(
"context"
"context"
"encoding/json"
"fmt"
"fmt"
"log"
"log"
"net/http"
"net/http"
...
@@ -27,6 +28,11 @@ type Input struct {
...
@@ -27,6 +28,11 @@ type Input struct {
func
main
()
{
func
main
()
{
mainQA
()
ctx
:=
context
.
Background
()
ctx
:=
context
.
Background
()
ds
:=
deepseek
.
DeepSeek
{
ds
:=
deepseek
.
DeepSeek
{
...
@@ -48,7 +54,13 @@ func main() {
...
@@ -48,7 +54,13 @@ func main() {
// Define a simple flow that generates jokes about a given topic
// Define a simple flow that generates jokes about a given topic
genkit
.
DefineFlow
(
g
,
"chat"
,
func
(
ctx
context
.
Context
,
input
*
Input
)
(
string
,
error
)
{
genkit
.
DefineFlow
(
g
,
"chat"
,
func
(
ctx
context
.
Context
,
input
*
Input
)
(
string
,
error
)
{
fmt
.
Println
(
"input-------------------------------"
,
input
.
Content
)
inputAsJson
,
err
:=
json
.
Marshal
(
input
)
if
err
!=
nil
{
return
""
,
err
}
fmt
.
Println
(
"input-------------------------------"
,
string
(
inputAsJson
))
resp
,
err
:=
genkit
.
Generate
(
ctx
,
g
,
resp
,
err
:=
genkit
.
Generate
(
ctx
,
g
,
ai
.
WithModel
(
m
),
ai
.
WithModel
(
m
),
...
@@ -80,12 +92,6 @@ func main() {
...
@@ -80,12 +92,6 @@ func main() {
mux
.
Handle
(
"POST /"
+
a
.
Name
(),
handler
)
mux
.
Handle
(
"POST /"
+
a
.
Name
(),
handler
)
}
}
//mux.HandleFunc("/swagger/", httpSwagger.WrapHandler)
// 暴露 Swagger UI,使用 swagger.yaml
// mux.HandleFunc("/swagger/", httpSwagger.Handler(
// httpSwagger.URL("/swagger/doc.yaml"), // 指定 YAML 文件路径
// ))
// 暴露 Swagger UI,使用 swagger.yaml
// 暴露 Swagger UI,使用 swagger.yaml
mux
.
HandleFunc
(
"/swagger/"
,
httpSwagger
.
Handler
(
mux
.
HandleFunc
(
"/swagger/"
,
httpSwagger
.
Handler
(
...
...
qa.go
View file @
a1665fde
...
@@ -15,7 +15,6 @@ var (
...
@@ -15,7 +15,6 @@ var (
connString
=
flag
.
String
(
"dbconn"
,
""
,
"database connection string"
)
connString
=
flag
.
String
(
"dbconn"
,
""
,
"database connection string"
)
)
)
// QA 结构体表示 qa 表的记录
type
QA
struct
{
type
QA
struct
{
ID
int64
// 主键
ID
int64
// 主键
CreatedAt
time
.
Time
// 创建时间
CreatedAt
time
.
Time
// 创建时间
...
@@ -23,6 +22,7 @@ type QA struct {
...
@@ -23,6 +22,7 @@ type QA struct {
Username
*
string
// 可空的用户名
Username
*
string
// 可空的用户名
Question
*
string
// 可空的问题
Question
*
string
// 可空的问题
Answer
*
string
// 可空的答案
Answer
*
string
// 可空的答案
Summary
*
string
// 可空的摘要
}
}
// QAStore 定义 DAO 接口
// QAStore 定义 DAO 接口
...
@@ -43,10 +43,9 @@ func NewQAStore(db *sql.DB) QAStore {
...
@@ -43,10 +43,9 @@ func NewQAStore(db *sql.DB) QAStore {
return
&
qaStore
{
db
:
db
}
return
&
qaStore
{
db
:
db
}
}
}
// GetLatestQA 从 latest_qa 视图读取数据
func
(
s
*
qaStore
)
GetLatestQA
(
ctx
context
.
Context
,
userID
*
int64
)
([]
QA
,
error
)
{
func
(
s
*
qaStore
)
GetLatestQA
(
ctx
context
.
Context
,
userID
*
int64
)
([]
QA
,
error
)
{
query
:=
`
query
:=
`
SELECT id, created_at, user_id, username, question, answer
SELECT id, created_at, user_id, username, question, answer
, summary
FROM latest_qa
FROM latest_qa
WHERE user_id = $1 OR (user_id IS NULL AND $1 IS NULL)`
WHERE user_id = $1 OR (user_id IS NULL AND $1 IS NULL)`
args
:=
[]
interface
{}{
userID
}
args
:=
[]
interface
{}{
userID
}
...
@@ -64,8 +63,8 @@ func (s *qaStore) GetLatestQA(ctx context.Context, userID *int64) ([]QA, error)
...
@@ -64,8 +63,8 @@ func (s *qaStore) GetLatestQA(ctx context.Context, userID *int64) ([]QA, error)
for
rows
.
Next
()
{
for
rows
.
Next
()
{
var
qa
QA
var
qa
QA
var
userIDVal
sql
.
NullInt64
var
userIDVal
sql
.
NullInt64
var
username
,
question
,
answer
sql
.
NullString
var
username
,
question
,
answer
,
summary
sql
.
NullString
if
err
:=
rows
.
Scan
(
&
qa
.
ID
,
&
qa
.
CreatedAt
,
&
userIDVal
,
&
username
,
&
question
,
&
answer
);
err
!=
nil
{
if
err
:=
rows
.
Scan
(
&
qa
.
ID
,
&
qa
.
CreatedAt
,
&
userIDVal
,
&
username
,
&
question
,
&
answer
,
&
summary
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"scan row: %w"
,
err
)
return
nil
,
fmt
.
Errorf
(
"scan row: %w"
,
err
)
}
}
if
userIDVal
.
Valid
{
if
userIDVal
.
Valid
{
...
@@ -80,6 +79,9 @@ func (s *qaStore) GetLatestQA(ctx context.Context, userID *int64) ([]QA, error)
...
@@ -80,6 +79,9 @@ func (s *qaStore) GetLatestQA(ctx context.Context, userID *int64) ([]QA, error)
if
answer
.
Valid
{
if
answer
.
Valid
{
qa
.
Answer
=
&
answer
.
String
qa
.
Answer
=
&
answer
.
String
}
}
if
summary
.
Valid
{
qa
.
Summary
=
&
summary
.
String
}
results
=
append
(
results
,
qa
)
results
=
append
(
results
,
qa
)
}
}
if
err
:=
rows
.
Err
();
err
!=
nil
{
if
err
:=
rows
.
Err
();
err
!=
nil
{
...
@@ -88,17 +90,16 @@ func (s *qaStore) GetLatestQA(ctx context.Context, userID *int64) ([]QA, error)
...
@@ -88,17 +90,16 @@ func (s *qaStore) GetLatestQA(ctx context.Context, userID *int64) ([]QA, error)
return
results
,
nil
return
results
,
nil
}
}
// WriteQA 插入或更新 qa 表记录
func
(
s
*
qaStore
)
WriteQA
(
ctx
context
.
Context
,
qa
QA
)
(
int64
,
error
)
{
func
(
s
*
qaStore
)
WriteQA
(
ctx
context
.
Context
,
qa
QA
)
(
int64
,
error
)
{
if
qa
.
ID
!=
0
{
if
qa
.
ID
!=
0
{
// 更新记录
// 更新记录
query
:=
`
query
:=
`
UPDATE qa
UPDATE qa
SET user_id = $1, username = $2, question = $3, answer = $4
SET user_id = $1, username = $2, question = $3, answer = $4
, summary = $5
WHERE id = $
5
WHERE id = $
6
RETURNING id`
RETURNING id`
var
updatedID
int64
var
updatedID
int64
err
:=
s
.
db
.
QueryRowContext
(
ctx
,
query
,
qa
.
UserID
,
qa
.
Username
,
qa
.
Question
,
qa
.
Answer
,
qa
.
ID
)
.
Scan
(
&
updatedID
)
err
:=
s
.
db
.
QueryRowContext
(
ctx
,
query
,
qa
.
UserID
,
qa
.
Username
,
qa
.
Question
,
qa
.
Answer
,
qa
.
Summary
,
qa
.
ID
)
.
Scan
(
&
updatedID
)
if
err
==
sql
.
ErrNoRows
{
if
err
==
sql
.
ErrNoRows
{
return
0
,
fmt
.
Errorf
(
"no record found with id %d"
,
qa
.
ID
)
return
0
,
fmt
.
Errorf
(
"no record found with id %d"
,
qa
.
ID
)
}
}
...
@@ -110,11 +111,11 @@ func (s *qaStore) WriteQA(ctx context.Context, qa QA) (int64, error) {
...
@@ -110,11 +111,11 @@ func (s *qaStore) WriteQA(ctx context.Context, qa QA) (int64, error) {
// 插入新记录
// 插入新记录
query
:=
`
query
:=
`
INSERT INTO qa (user_id, username, question, answer)
INSERT INTO qa (user_id, username, question, answer
, summary
)
VALUES ($1, $2, $3, $4)
VALUES ($1, $2, $3, $4
, $5
)
RETURNING id`
RETURNING id`
var
newID
int64
var
newID
int64
err
:=
s
.
db
.
QueryRowContext
(
ctx
,
query
,
qa
.
UserID
,
qa
.
Username
,
qa
.
Question
,
qa
.
Answer
)
.
Scan
(
&
newID
)
err
:=
s
.
db
.
QueryRowContext
(
ctx
,
query
,
qa
.
UserID
,
qa
.
Username
,
qa
.
Question
,
qa
.
Answer
,
qa
.
Summary
)
.
Scan
(
&
newID
)
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
fmt
.
Errorf
(
"insert qa: %w"
,
err
)
return
0
,
fmt
.
Errorf
(
"insert qa: %w"
,
err
)
}
}
...
@@ -122,14 +123,15 @@ func (s *qaStore) WriteQA(ctx context.Context, qa QA) (int64, error) {
...
@@ -122,14 +123,15 @@ func (s *qaStore) WriteQA(ctx context.Context, qa QA) (int64, error) {
}
}
func
mainQA
()
{
func
mainQA
()
{
flag
.
Parse
()
ctx
:=
context
.
Background
()
if
*
connString
==
""
{
// postgres://postgres.awcfgdodiuqnlsobcivq:[YOUR-PASSWORD]@aws-0-ap-southeast-1.pooler.supabase.com:6543/postgres
log
.
Fatal
(
"need -dbconn"
)
// postgres://postgres.awcfgdodiuqnlsobcivq:rNVrqxaapXypEGg8@aws-0-ap-southeast-1.pooler.supabase.com:6543/postgres
}
//connString := "postgres://postgres.awcfgdodiuqnlsobcivq:rNVrqxaapXypEGg8@aws-0-ap-southeast-1.pooler.supabase.com:6543/postgres"
// var connString = "postgres://postgres.awcfgdodiuqnlsobcivq:rNVrqxaapXypEGg8@aws-0-ap-southeast-1.pooler.supabase.com:6543/postgres"
var
connString
=
"postgresql://postgres.awcfgdodiuqnlsobcivq:P8L00j7k3wHRwUJw@aws-0-ap-southeast-1.pooler.supabase.com:6543/postgres"
db
,
err
:=
sql
.
Open
(
"postgres"
,
*
connString
)
db
,
err
:=
sql
.
Open
(
"postgres"
,
connString
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"open database: %v"
,
err
)
log
.
Fatalf
(
"open database: %v"
,
err
)
}
}
...
@@ -137,14 +139,17 @@ func mainQA() {
...
@@ -137,14 +139,17 @@ func mainQA() {
store
:=
NewQAStore
(
db
)
store
:=
NewQAStore
(
db
)
ctx
:=
context
.
Background
()
// 示例:读取 user_id=101 的最新 QA
// 示例:读取 user_id=101 的最新 QA
results
,
err
:=
store
.
GetLatestQA
(
ctx
,
int64Ptr
(
101
))
results
,
err
:=
store
.
GetLatestQA
(
ctx
,
int64Ptr
(
101
))
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"get latest QA: %v"
,
err
)
log
.
Fatalf
(
"get latest QA: %v"
,
err
)
}
}
for
_
,
qa
:=
range
results
{
for
_
,
qa
:=
range
results
{
fmt
.
Printf
(
"ID: %d, CreatedAt: %v, UserID: %v, Username: %v, Question: %v, Answer: %v
\n
"
,
fmt
.
Printf
(
"ID: %d, CreatedAt: %v, UserID: %v, Username: %v, Question: %v, Answer: %v, Summary: %v
\n
"
,
qa
.
ID
,
qa
.
CreatedAt
,
derefInt64
(
qa
.
UserID
),
derefString
(
qa
.
Username
),
derefString
(
qa
.
Question
),
derefString
(
qa
.
Answer
))
qa
.
ID
,
qa
.
CreatedAt
,
derefInt64
(
qa
.
UserID
),
derefString
(
qa
.
Username
),
derefString
(
qa
.
Question
),
derefString
(
qa
.
Answer
),
derefString
(
qa
.
Summary
))
}
}
// 示例:插入新 QA
// 示例:插入新 QA
...
@@ -153,6 +158,7 @@ func mainQA() {
...
@@ -153,6 +158,7 @@ func mainQA() {
Username
:
stringPtr
(
"alice"
),
Username
:
stringPtr
(
"alice"
),
Question
:
stringPtr
(
"What is AI?"
),
Question
:
stringPtr
(
"What is AI?"
),
Answer
:
stringPtr
(
"AI is..."
),
Answer
:
stringPtr
(
"AI is..."
),
Summary
:
stringPtr
(
"AI overview"
),
}
}
newID
,
err
:=
store
.
WriteQA
(
ctx
,
newQA
)
newID
,
err
:=
store
.
WriteQA
(
ctx
,
newQA
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -167,6 +173,7 @@ func mainQA() {
...
@@ -167,6 +173,7 @@ func mainQA() {
Username
:
stringPtr
(
"alice_updated"
),
Username
:
stringPtr
(
"alice_updated"
),
Question
:
stringPtr
(
"What is NLP?"
),
Question
:
stringPtr
(
"What is NLP?"
),
Answer
:
stringPtr
(
"NLP is..."
),
Answer
:
stringPtr
(
"NLP is..."
),
Summary
:
stringPtr
(
"NLP overview"
),
}
}
updatedID
,
err
:=
store
.
WriteQA
(
ctx
,
updateQA
)
updatedID
,
err
:=
store
.
WriteQA
(
ctx
,
updateQA
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -175,6 +182,7 @@ func mainQA() {
...
@@ -175,6 +182,7 @@ func mainQA() {
fmt
.
Printf
(
"Updated QA with ID: %d
\n
"
,
updatedID
)
fmt
.
Printf
(
"Updated QA with ID: %d
\n
"
,
updatedID
)
}
}
// 辅助函数:处理指针类型的空值
// 辅助函数:处理指针类型的空值
func
int64Ptr
(
i
int64
)
*
int64
{
func
int64Ptr
(
i
int64
)
*
int64
{
return
&
i
return
&
i
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment