-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprompt_api.py
More file actions
169 lines (144 loc) · 6.34 KB
/
prompt_api.py
File metadata and controls
169 lines (144 loc) · 6.34 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: UTF-8 -*-
from typing import List
from langchain.output_parsers import pydantic
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, Integer, String, DateTime, func
from kb_api import Prompt
from config import config
from kb_api import with_session
from fastapi.responses import Response
from fastapi import Body
from loguru import logger
@with_session
def add_prompt_in_sqlite(session,
name: str,
template:str
):
p = session.query(Prompt).filter_by(name=name).first()
if p :
return False
else :
prompt=Prompt(name=name,template=template)
session.add(prompt)
return True
@with_session
def list_prompt_from_db(session):
prompts = session.query(Prompt.name).filter( ).all()
ps = [p[0] for p in prompts]
return ps
@with_session
def update_prompt_from_db(session, name,content):
# 找到名为John的prompt,并更新他
session.query(Prompt).filter(Prompt.name == name).update({Prompt.template: content}, synchronize_session=False)
# 提交更改
session.commit()
@with_session
def load_prompt_from_db(session, name):
# if name=='rag_default':
# rag_default_prompt_content= '''
# You are an AI assistant who is helpful, respectful, and honest.
# Use the below given context to answer the customer queries.
# If there is anything that you cannot answer, or you think is inappropriate to answer, simply reply as,"Sorry, I cannot help you with that."
# CONTEXT: {context}
#
# Instructions:
# 1. Answer only from the given context.
# 2: Please answer the question simply as you can, and do not generate any new content out of this context.
# 3: Your answer should not include any harmful, unethical, violent, racist, sexist, pornographic, toxic, discriminatory, blasphemous, dangerous, or illegal content.
# 4: Please ensure that your responses are socially unbiased and positive in nature.
# 5: Ensure length of the answer is within 300 words.
# Now, Answer the following question: {question}
# '''
# return rag_default_prompt_content
kb = session.query(Prompt).filter_by(name=name).first()
if kb:
return kb.template
else:
return None
@with_session
def delete_prompt_from_db(session, name):
prompt = session.query(Prompt).filter_by(name=name).first()
if prompt:
session.delete(prompt)
session.commit()
else:
return None
from fastapi import Form
from pydantic import BaseModel
import pydantic
class PromptResponse(BaseModel):
data: str = pydantic.Field(..., description="data returned from vector store")
status: str = pydantic.Field(..., description="Response text")
err_msg: str = pydantic.Field(..., description="Response text")
class Config:
json_schema_extra = {
"example": {
"data": [{"content": "xxx", "score": 1, "source": "llm"},
{"content": "yyy", "score": 0.78, "source": "source file url"}],
"status": "success",
"err_msg": ""
}
}
from util import BaseResponse
class ListResponse(BaseResponse):
data: List[str] = pydantic.Field(..., description="List of knowledge base names")
class Config:
json_schema_extra = {
"example": {
"code": 200,
"msg": "success",
"data": ["bank", "medical", "OCI info"],
}
}
def list_prompts():
arr=list_prompt_from_db()
# arr.append('default')
# arr.append('rag_default')
return ListResponse(data=list(arr))
def get_prompt( name: str = Form(..., description="prompt name", examples=["llama2Prompt"]),
) -> Response:
kk= load_prompt_from_db(name)
if kk:
return Response(content=kk, media_type="text/plain")
else:
return Response(content='{query}', media_type="text/plain")
def delete_prompt(
name: str = Form(..., description="prompt name", examples=["llama2Prompt"]),
):
logger.info(f"##delete_prompt name:{name}")
delete_prompt_from_db(name)
return PromptResponse(data=f"deleted prompt {name} ", status="ok",err_msg="")
def add_prompt(
name: str = Form(..., description="prompt name", examples=["llama2Prompt"]),
template: str = Form(..., description="when you chat with llm, {query} is variable, chat with rag, {question} {context} are variables", examples=["you are an AI, answer my question {query}"]),
) -> Response:
logger.info(f"##name:{name} template:{template}")
kk= load_prompt_from_db(name)
logger.info(f"##kk:{kk} ")
if kk:
return PromptResponse(data=f"the prompt existed, change another name ", status="ok",err_msg="change another name")
else:
add_prompt_in_sqlite(name,template)
return PromptResponse(data=f"created prompt {name} ", status="ok",err_msg="")
def update_prompt(
name: str = Form(..., description="prompt name", examples=["llama2Prompt"]),
template: str = Form(..., description="when you chat with llm, {query} is variable, chat with rag, {question} {context} are variables", examples=["you are an AI, answer my question {query}"]),
) -> PromptResponse:
update_prompt_from_db(name,template)
return PromptResponse(status="ok" , data=f"updated prompt {name} ",err_msg="")
def init_default():
add_prompt_in_sqlite('default','{query}')
rag_default_prompt_content = '''
You are an AI assistant who is helpful, respectful, and honest.
Use the below given context to answer the customer queries.
If there is anything that you cannot answer, or you think is inappropriate to answer, simply reply as,"Sorry, I cannot help you with that."
CONTEXT: {context}
Instructions:
1. Answer only from the given context.
2: Please answer the question simply as you can, and do not generate any new content out of this context.
3: Your answer should not include any harmful, unethical, violent, racist, sexist, pornographic, toxic, discriminatory, blasphemous, dangerous, or illegal content.
4: Please ensure that your responses are socially unbiased and positive in nature.
5: Ensure length of the answer is within 300 words.
Now, Answer the following question: {question}
'''
add_prompt_in_sqlite('rag_default',rag_default_prompt_content)