martinezjandrew commited on
Commit
8010521
·
1 Parent(s): 71b8ef4

Added new pipeline for updating a script

Browse files
app.py CHANGED
@@ -10,18 +10,46 @@ async def root():
10
  return {"message": "Hello World"}
11
 
12
 
13
- class Query(BaseModel):
14
  topic: str
15
  time: str
16
 
17
 
18
- @app.post("/test")
19
- async def test(query: Query):
20
- with open("./pipelines/test.yml", "r") as file:
21
  content = file.read()
22
 
23
  pipe = Pipeline.loads(content)
24
 
25
- results = pipe.run({"prompt_builder": {"topic": query.topic, "time": query.time}})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  return {"response": results}
 
10
  return {"message": "Hello World"}
11
 
12
 
13
+ class ScriptRequest(BaseModel):
14
  topic: str
15
  time: str
16
 
17
 
18
+ @app.post("/generate-script")
19
+ async def generate_script(req: ScriptRequest):
20
+ with open("./pipelines/generate_script.yml", "r") as file:
21
  content = file.read()
22
 
23
  pipe = Pipeline.loads(content)
24
 
25
+ results = pipe.run(
26
+ {
27
+ "prompt_builder": {"topic": req.topic, "time": req.time},
28
+ }
29
+ )
30
+
31
+ return {"response": results}
32
+
33
+
34
+ class ScriptUpdate(BaseModel):
35
+ script: str
36
+ notes: str = ""
37
+
38
+
39
+ @app.post("/update-script")
40
+ async def update_script(req: ScriptUpdate):
41
+ with open("./pipelines/update_script.yml", "r") as file:
42
+ content = file.read()
43
+
44
+ pipe = Pipeline.loads(content)
45
+
46
+ results = pipe.run(
47
+ {
48
+ "prompt_builder": {
49
+ "script": req.script,
50
+ "notes": req.notes,
51
+ },
52
+ }
53
+ )
54
 
55
  return {"response": results}
pipelines/{pipeline1.py → generate_script.py} RENAMED
@@ -30,7 +30,7 @@ pipeline.add_component("prompt_builder", PromptBuilder(template=template))
30
  pipeline.add_component("llm", generator)
31
  pipeline.connect("prompt_builder", "llm")
32
 
33
- with open("test.yml", "w") as file:
34
  pipeline.dump(file)
35
  """
36
  results = pipeline.run(
 
30
  pipeline.add_component("llm", generator)
31
  pipeline.connect("prompt_builder", "llm")
32
 
33
+ with open("generate_script.yml", "w") as file:
34
  pipeline.dump(file)
35
  """
36
  results = pipeline.run(
pipelines/{test.yml → generate_script.yml} RENAMED
@@ -5,19 +5,19 @@ components:
5
  model: meta-llama/Meta-Llama-3-8B-Instruct
6
  api_type: serverless_inference_api
7
  generation_kwargs:
8
- max_new_tokens: 512
9
  stop_sequences: []
10
  streaming_callback: null
11
  token:
12
  env_vars:
13
- - HF_API_TOKEN
14
  strict: true
15
  type: env_var
16
  type: haystack.components.generators.hugging_face_api.HuggingFaceAPIGenerator
17
  prompt_builder:
18
  init_parameters:
19
  required_variables: null
20
- template: '
21
 
22
  You are a training manager. You are tasked to create an online, asynchronous
23
  course for new employees. Create an outline for a course about {{ topic }}
@@ -41,11 +41,11 @@ components:
41
 
42
  - Conclusion
43
 
44
- '
45
  variables: null
46
  type: haystack.components.builders.prompt_builder.PromptBuilder
47
  connections:
48
- - receiver: llm.prompt
49
- sender: prompt_builder.prompt
50
  max_runs_per_component: 100
51
  metadata: {}
 
5
  model: meta-llama/Meta-Llama-3-8B-Instruct
6
  api_type: serverless_inference_api
7
  generation_kwargs:
8
+ max_new_tokens: 1028
9
  stop_sequences: []
10
  streaming_callback: null
11
  token:
12
  env_vars:
13
+ - HF_API_TOKEN
14
  strict: true
15
  type: env_var
16
  type: haystack.components.generators.hugging_face_api.HuggingFaceAPIGenerator
17
  prompt_builder:
18
  init_parameters:
19
  required_variables: null
20
+ template: "
21
 
22
  You are a training manager. You are tasked to create an online, asynchronous
23
  course for new employees. Create an outline for a course about {{ topic }}
 
41
 
42
  - Conclusion
43
 
44
+ "
45
  variables: null
46
  type: haystack.components.builders.prompt_builder.PromptBuilder
47
  connections:
48
+ - receiver: llm.prompt
49
+ sender: prompt_builder.prompt
50
  max_runs_per_component: 100
51
  metadata: {}
pipelines/update_script.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from haystack import Pipeline
2
+ from haystack.components.builders import PromptBuilder
3
+ from haystack.components.generators import HuggingFaceAPIGenerator
4
+ from haystack.utils import Secret
5
+
6
+ template = """
7
+ You are a training manager. You are tasked to create an online, asynchronous course for new employees. \
8
+ You previously generated a script and provided it to the user. The user requested some changes to be made. \
9
+
10
+ The user provided the following feedback:
11
+ {{ notes }}
12
+
13
+ Here's the script you previously created with possible changes made by the user.
14
+ {{ script }}
15
+
16
+ The outline should include the following:
17
+ - Course Objectives
18
+ - Main Topics
19
+ - Subtopics for each Main Topic
20
+ - Interactive Activities (if applicable)
21
+ - Estimated time for each topic within {{ time }}
22
+ - Assessments or quizzes (if any)
23
+ - Additional Resources
24
+ - Conclusion
25
+ """
26
+
27
+ builder = PromptBuilder(template=template)
28
+
29
+ generator = HuggingFaceAPIGenerator(
30
+ api_type="serverless_inference_api",
31
+ api_params={"model": "meta-llama/Meta-Llama-3-8B-Instruct"},
32
+ token=Secret.from_env_var("HF_API_TOKEN"),
33
+ )
34
+
35
+ pipeline = Pipeline()
36
+ pipeline.add_component("prompt_builder", PromptBuilder(template=template))
37
+ pipeline.add_component("llm", generator)
38
+ pipeline.connect("prompt_builder", "llm")
39
+
40
+ with open("update_script.yml", "w") as file:
41
+ pipeline.dump(file)
42
+ """
43
+ results = pipeline.run(
44
+ {"prompt_builder": {"topic": "Artificial Intelligence", "time": "one hour"}}
45
+ )
46
+ print(results)
47
+ """
pipelines/update_script.yml ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ components:
2
+ llm:
3
+ init_parameters:
4
+ api_params:
5
+ model: meta-llama/Meta-Llama-3-8B-Instruct
6
+ api_type: serverless_inference_api
7
+ generation_kwargs:
8
+ max_new_tokens: 1028
9
+ stop_sequences: []
10
+ streaming_callback: null
11
+ token:
12
+ env_vars:
13
+ - HF_API_TOKEN
14
+ strict: true
15
+ type: env_var
16
+ type: haystack.components.generators.hugging_face_api.HuggingFaceAPIGenerator
17
+ prompt_builder:
18
+ init_parameters:
19
+ required_variables: null
20
+ template:
21
+ "\nYou are a training manager. You are tasked to create an online,\
22
+ \ asynchronous course for new employees. You previously generated a script\
23
+ \ and provided it to the user. The user requested some changes to be made.\
24
+ \ \nThe user provided the following feedback:\n{{ notes }}\n\nHere's the script\
25
+ \ you previously created with possible changes made by the user.\n{{ script\
26
+ \ }}\n\nThe outline should include the following:\n- Course Objectives\n-\
27
+ \ Main Topics\n- Subtopics for each Main Topic\n- Interactive Activities (if\
28
+ \ applicable)\n- Estimated time for each topic within {{ time }}\n- Assessments\
29
+ \ or quizzes (if any)\n- Additional Resources\n- Conclusion\n"
30
+ variables: null
31
+ type: haystack.components.builders.prompt_builder.PromptBuilder
32
+ connections:
33
+ - receiver: llm.prompt
34
+ sender: prompt_builder.prompt
35
+ max_runs_per_component: 100
36
+ metadata: {}