Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

Commit 5aa7ff4

Browse files
committed
refactor get prompt service
1 parent 53a995f commit 5aa7ff4

2 files changed

Lines changed: 171 additions & 31 deletions

File tree

dummy.ipynb

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "initial_id",
7+
"metadata": {
8+
"collapsed": true,
9+
"ExecuteTime": {
10+
"end_time": "2023-10-05T13:43:29.749167090Z",
11+
"start_time": "2023-10-05T13:43:24.752225360Z"
12+
}
13+
},
14+
"outputs": [],
15+
"source": [
16+
"from genai_stack.stack.stack import Stack\n",
17+
"from genai_stack.model import HuggingFaceModel\n",
18+
"from genai_stack.etl.langchain import LangchainETL\n",
19+
"from genai_stack.embedding.langchain import LangchainEmbedding\n",
20+
"from genai_stack.vectordb.chromadb import ChromaDB\n",
21+
"from genai_stack.prompt_engine.engine import PromptEngine\n",
22+
"from genai_stack.retriever.langchain import LangChainRetriever\n",
23+
"from genai_stack.memory.langchain import ConversationBufferMemory"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 2,
29+
"outputs": [],
30+
"source": [
31+
"etl = LangchainETL.from_kwargs(\n",
32+
" name=\"WebBaseLoader\",\n",
33+
" fields={\"web_path\": [\n",
34+
" \"https://aiplanet.com\",\n",
35+
" ]\n",
36+
" }\n",
37+
")"
38+
],
39+
"metadata": {
40+
"collapsed": false,
41+
"ExecuteTime": {
42+
"end_time": "2023-10-05T13:43:29.752762828Z",
43+
"start_time": "2023-10-05T13:43:29.749885841Z"
44+
}
45+
},
46+
"id": "58197b4e9c357f27"
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 5,
51+
"outputs": [],
52+
"source": [
53+
"llm = HuggingFaceModel.from_kwargs(model=\"skt/ko-gpt-trinity-1.2B-v0.5\")\n",
54+
"config = {\n",
55+
" \"model_name\": \"sentence-transformers/all-mpnet-base-v2\",\n",
56+
" \"model_kwargs\": {\"device\": \"cpu\"},\n",
57+
" \"encode_kwargs\": {\"normalize_embeddings\": False},\n",
58+
"}\n",
59+
"embedding = LangchainEmbedding.from_kwargs(name=\"HuggingFaceEmbeddings\", fields=config)\n",
60+
"chromadb = ChromaDB.from_kwargs()"
61+
],
62+
"metadata": {
63+
"collapsed": false,
64+
"ExecuteTime": {
65+
"end_time": "2023-10-05T13:44:37.250590Z",
66+
"start_time": "2023-10-05T13:44:37.223926726Z"
67+
}
68+
},
69+
"id": "42fa2dcd7085ac80"
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 1,
74+
"outputs": [
75+
{
76+
"ename": "NameError",
77+
"evalue": "name 'PromptEngine' is not defined",
78+
"output_type": "error",
79+
"traceback": [
80+
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
81+
"\u001B[0;31mNameError\u001B[0m Traceback (most recent call last)",
82+
"Cell \u001B[0;32mIn[1], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m prompt_engine \u001B[38;5;241m=\u001B[39m \u001B[43mPromptEngine\u001B[49m\u001B[38;5;241m.\u001B[39mfrom_kwargs(should_validate\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mFalse\u001B[39;00m)\n\u001B[1;32m 2\u001B[0m retriever \u001B[38;5;241m=\u001B[39m LangChainRetriever\u001B[38;5;241m.\u001B[39mfrom_kwargs()\n\u001B[1;32m 3\u001B[0m memory \u001B[38;5;241m=\u001B[39m ConversationBufferMemory\u001B[38;5;241m.\u001B[39mfrom_kwargs()\n",
83+
"\u001B[0;31mNameError\u001B[0m: name 'PromptEngine' is not defined"
84+
]
85+
}
86+
],
87+
"source": [
88+
"prompt_engine = PromptEngine.from_kwargs(should_validate=False)\n",
89+
"retriever = LangChainRetriever.from_kwargs()\n",
90+
"memory = ConversationBufferMemory.from_kwargs()\n",
91+
"Stack(\n",
92+
" etl=etl,\n",
93+
" embedding=embedding,\n",
94+
" vectordb=chromadb,\n",
95+
" model=llm,\n",
96+
" prompt_engine=prompt_engine,\n",
97+
" retriever=retriever,\n",
98+
" memory=memory\n",
99+
")\n"
100+
],
101+
"metadata": {
102+
"collapsed": false,
103+
"ExecuteTime": {
104+
"end_time": "2023-10-05T13:45:48.582398487Z",
105+
"start_time": "2023-10-05T13:45:48.417937857Z"
106+
}
107+
},
108+
"id": "81e991b63b233e6d"
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": null,
113+
"outputs": [],
114+
"source": [
115+
"prompt1 = \"Why choose models from AI Marketplace?\""
116+
],
117+
"metadata": {
118+
"collapsed": false,
119+
"ExecuteTime": {
120+
"start_time": "2023-10-05T13:43:49.783528419Z"
121+
}
122+
},
123+
"id": "2e815c9b106810f0"
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": null,
128+
"outputs": [],
129+
"source": [],
130+
"metadata": {
131+
"collapsed": false,
132+
"ExecuteTime": {
133+
"start_time": "2023-10-05T13:43:49.786045019Z"
134+
}
135+
},
136+
"id": "80d05b20a6921f73"
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": null,
141+
"outputs": [],
142+
"source": [],
143+
"metadata": {
144+
"collapsed": false
145+
},
146+
"id": "8226f4204a117563"
147+
}
148+
],
149+
"metadata": {
150+
"kernelspec": {
151+
"display_name": "Python 3",
152+
"language": "python",
153+
"name": "python3"
154+
},
155+
"language_info": {
156+
"codemirror_mode": {
157+
"name": "ipython",
158+
"version": 2
159+
},
160+
"file_extension": ".py",
161+
"mimetype": "text/x-python",
162+
"name": "python",
163+
"nbconvert_exporter": "python",
164+
"pygments_lexer": "ipython2",
165+
"version": "2.7.6"
166+
}
167+
},
168+
"nbformat": 4,
169+
"nbformat_minor": 5
170+
}

genai_stack/genai_server/services/prompt_engine_service.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,7 @@ def get_prompt(self, data: PromptEngineGetRequestModel) -> PromptEngineGetRespon
2121
stack_session = session.get(StackSessionSchema, data.session_id)
2222
if stack_session is None:
2323
raise HTTPException(status_code=404, detail=f"Session {data.session_id} not found")
24-
prompt_session = (
25-
session.query(PromptSchema)
26-
.filter_by(stack_session=data.session_id, type=data.type.value)
27-
.first()
28-
)
29-
if prompt_session is not None:
30-
template = prompt_session.template
31-
prompt_type_map = {
32-
PromptTypeEnum.SIMPLE_CHAT_PROMPT.value: "simple_chat_prompt_template",
33-
PromptTypeEnum.CONTEXTUAL_CHAT_PROMPT.value: "contextual_chat_prompt_template",
34-
PromptTypeEnum.CONTEXTUAL_QA_PROMPT.value: "contextual_qa_prompt_template",
35-
}
36-
input_variables = ["context", "history", "query"]
37-
if data.type == PromptTypeEnum.SIMPLE_CHAT_PROMPT:
38-
input_variables.remove("context")
39-
elif data.type == PromptTypeEnum.CONTEXTUAL_QA_PROMPT:
40-
input_variables.remove("history")
41-
prompt = PromptTemplate(template=template, input_variables=input_variables)
42-
stack = get_current_stack(
43-
engine=session,
44-
config=stack_config,
45-
session=stack_session,
46-
overide_config={
47-
"prompt_engine": {
48-
"should_validate": data.should_validate,
49-
prompt_type_map[data.type.value]: prompt
50-
}
51-
}
52-
)
53-
else:
54-
stack = get_current_stack(config=stack_config, engine=session, session=stack_session)
24+
stack = get_current_stack(config=stack_config, engine=session, session=stack_session)
5525
prompt = stack.prompt_engine.get_prompt_template(promptType=data.type, query=data.query)
5626
return PromptEngineGetResponseModel(
5727
template=prompt.template,

0 commit comments

Comments
 (0)