123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package chat
- import (
- "fmt"
- "os"
- "strings"
- "sync"
- "time"
- "trial/chat/api"
- "github.com/wecisecode/util/merrs"
- "github.com/wecisecode/util/mfmt"
- )
- type Chat struct {
- }
- func NewChat() *Chat {
- return &Chat{}
- }
- var questions = []string{
- // `基于以下上下文,精确回答问题。如果上下文不包含答案,回答'未知'。\n上下文:{context}\n问题:{question}"`,
- `基于以下信息,回答稍后的问题:
-
- 昵称 姓名 M N K
- 班长 王敏 01065500556 13612345678 110123197012121234
- 博士 李志刚 01065436543 13387654321 120123197002020202
- `,
- // `syslog sys/5/linkupdown 告警具有什么样的含义,提供一下解决方案`,
- // `设备xxx的端口的对端链接端口是什么?`,
- // `设备xxx与设备xxx的连接关系是什么?`,
- // `设备xxx的最新配置是什么?`,
- // `设备xxx最近一年都有哪些变更?`,
- // `设备xxx如果希望通过ospf协议,通过xxx端口,对端设备xxx 对端端口xxx,给我生成一下配置`,
- // `由xxx变更的设备有哪些`,
- // `xxx最近1个月有哪些告警`,
- // `xxx设备和yyy设备的配置有哪些不同`,
- // `设备xxx最近一次ping不通是什么时候`,
- }
- func (chat *Chat) Run() error {
- question := ""
- for i := 1; i < len(os.Args); i++ {
- arg := os.Args[i]
- if !strings.HasPrefix(arg, "-") {
- question = arg
- }
- }
- if question != "" {
- questions = append([]string{question}, questions...)
- }
- response := ""
- var mutex sync.Mutex
- waiting := false
- senttime := time.Now()
- swaitingloopcount := ""
- go func() {
- for {
- mutex.Lock()
- if waiting {
- fmt.Print("\r", strings.Repeat(" ", len(swaitingloopcount)), "\r")
- swaitingloopcount = "等待服务响应 " + mfmt.FormatDuration(time.Since(senttime))
- fmt.Print(swaitingloopcount)
- }
- mutex.Unlock()
- time.Sleep(1 * time.Second)
- }
- }()
- context := []int64{}
- for {
- if len(questions) > 0 {
- question = questions[0]
- questions = questions[1:]
- fmt.Println(":=>- " + question)
- } else {
- fmt.Print(":=>- ")
- _, e := fmt.Scan(&question)
- if e != nil {
- return merrs.New(e)
- }
- }
- mutex.Lock()
- waiting = true
- senttime = time.Now()
- swaitingloopcount = ""
- mutex.Unlock()
- // q := `你是一个专业网络管理AI助手,请根据以下规则响应:
- // 1、如果用户问题是关于网络管理相关数据请求的,那么将用户问题转换为JSON对象格式,否则直接回答用户问题即可
- // 2、如果无法确定数据请求的任何条件,那么不要输出JSON对象,而应该引导用户提出与数据请求相关的问题
- // 3、JSON对象的字段说明如下:
- // type:要请求的数据的节点类型,包括 1 代表服务器分区, 2 代表交换机,3 代表路由器,0 代表所有节点
- // node:明确相关服务器的名称或IP地址,可以包含通配符,*代表任意字符串
- // status:要请求的数据的相关状态,包括 1 正常,2 不正常,3已关机
- // time:要请求的数据的相关时间段,用中文描述
- // 用户问题:` + question + `
- // `
- q := question
- // 现在时间是 "` + time.Now().Format("2006-01-02 15:04:05") + `"
- var responsestarttime time.Time
- for ret := range api.Request(context, q) {
- if ret.Error != nil {
- return ret.Error
- }
- mutex.Lock()
- if waiting {
- responsestarttime = time.Now()
- waiting = false
- fmt.Println()
- }
- mutex.Unlock()
- response += strings.TrimSpace(ret.Response.Response)
- fmt.Print(ret.Response.Response)
- if ret.Response.Done {
- fmt.Println()
- // fmt.Println(ret.Response.Context)
- fmt.Println("context length:", len(ret.Response.Context), ", response duration:", mfmt.FormatDuration(time.Since(responsestarttime)))
- context = ret.Response.Context
- break
- }
- }
- }
- }
|