Tachi67 commited on
Commit
54451bb
·
1 Parent(s): 174d5f8

Update PlanGeneratorAtomicFlow.py

Browse files
Files changed (1) hide show
  1. PlanGeneratorAtomicFlow.py +31 -1
PlanGeneratorAtomicFlow.py CHANGED
@@ -5,8 +5,13 @@ from flow_modules.aiflows.OpenAIChatFlowModule import OpenAIChatAtomicFlow
5
 
6
  from dataclasses import dataclass
7
  class PlanGeneratorAtomicFlow(OpenAIChatAtomicFlow):
8
- def __init__(self, **kwargs):
 
 
 
9
  super().__init__(**kwargs)
 
 
10
 
11
  @classmethod
12
  def instantiate_from_config(cls, config):
@@ -18,9 +23,28 @@ class PlanGeneratorAtomicFlow(OpenAIChatAtomicFlow):
18
  kwargs.update(cls._set_up_prompts(flow_config))
19
  kwargs.update(cls._set_up_backend(flow_config))
20
 
 
 
 
 
21
  # ~~~ Instantiate flow ~~~
22
  return cls(**kwargs)
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
25
  hint_for_model = """
26
  Make sure your response is in the following format:
@@ -31,6 +55,12 @@ class PlanGeneratorAtomicFlow(OpenAIChatAtomicFlow):
31
  """
32
  if 'goal' in input_data:
33
  input_data['goal'] += hint_for_model
 
 
 
 
 
 
34
  api_output = super().run(input_data)["api_output"].strip()
35
  try:
36
  response = json.loads(api_output)
 
5
 
6
  from dataclasses import dataclass
7
  class PlanGeneratorAtomicFlow(OpenAIChatAtomicFlow):
8
+ def __init__(
9
+ self,
10
+ code_file_location: str,
11
+ **kwargs):
12
  super().__init__(**kwargs)
13
+ self.code_file_location = code_file_location
14
+ self.original_system_template = self.system_message_prompt_template.template
15
 
16
  @classmethod
17
  def instantiate_from_config(cls, config):
 
23
  kwargs.update(cls._set_up_prompts(flow_config))
24
  kwargs.update(cls._set_up_backend(flow_config))
25
 
26
+ # ~~~ Set up code file location
27
+ code_file_location = flow_config["code_file_location"]
28
+ kwargs.update({"code_file_location": code_file_location})
29
+
30
  # ~~~ Instantiate flow ~~~
31
  return cls(**kwargs)
32
 
33
+ def _get_library_function_signatures(self):
34
+ import importlib.util
35
+ try:
36
+ spec = importlib.util.spec_from_file_location("code_library", self.code_file_location)
37
+ module = importlib.util.module_from_spec(spec)
38
+ spec.loader.exec_module(module)
39
+ ret = ''
40
+ import inspect
41
+ for name, obj in inspect.getmembers(module):
42
+ if inspect.isfunction(obj):
43
+ ret += f"{name}: {inspect.signature(obj)}\n"
44
+ return ret
45
+ except FileNotFoundError:
46
+ return 'There is no function available yet.'
47
+
48
  def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
49
  hint_for_model = """
50
  Make sure your response is in the following format:
 
55
  """
56
  if 'goal' in input_data:
57
  input_data['goal'] += hint_for_model
58
+
59
+ function_signatures_to_append = self._get_library_function_signatures()
60
+ self.system_message_prompt_template.template = \
61
+ self.original_system_template + "\n\n" + f"Here are the available functions at {self.code_file_location}\n" \
62
+ + function_signatures_to_append + "\n" \
63
+
64
  api_output = super().run(input_data)["api_output"].strip()
65
  try:
66
  response = json.loads(api_output)