Skip to content

Commit 918eac6

Browse files
committed
reorganize prompts to get rid of some errors
1 parent dadf02f commit 918eac6

28 files changed

+121
-159
lines changed

python/packages/ai/examples/openai_planner.py

-50
This file was deleted.

python/packages/ai/scripts/fmt.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88

99
def fmt():
10-
subprocess.run(["poetry", "run", "black", "teams", "scripts", "tests", "examples"], check=True)
11-
subprocess.run(["poetry", "run", "isort", "teams", "scripts", "tests", "examples"], check=True)
10+
subprocess.run(["poetry", "run", "black", "teams", "scripts", "tests"], check=True)
11+
subprocess.run(["poetry", "run", "isort", "teams", "scripts", "tests"], check=True)

python/packages/ai/scripts/lint.py

-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ def lint():
1010
subprocess.run(["poetry", "run", "pylint", "teams", "scripts", "tests"], check=True)
1111
subprocess.run(["poetry", "run", "mypy", "--check-untyped-defs", "-p", "teams"], check=True)
1212
subprocess.run(["poetry", "run", "mypy", "--check-untyped-defs", "-p", "tests"], check=True)
13-
subprocess.run(["poetry", "run", "mypy", "--check-untyped-defs", "-p", "examples"], check=True)

python/packages/ai/teams/ai/data_sources/data_source.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from botbuilder.core import TurnContext
99

10-
from teams.ai.prompts.prompt_section import RenderedPromptSection
10+
from teams.ai.prompts.rendered_prompt_section import RenderedPromptSection
1111
from teams.ai.tokenizers import Tokenizer
1212
from teams.state.memory import Memory
1313

python/packages/ai/teams/ai/data_sources/text_data_source.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from botbuilder.core import TurnContext
99

1010
from teams.ai.data_sources.data_source import DataSource
11-
from teams.ai.prompts.prompt_section import RenderedPromptSection
11+
from teams.ai.prompts.rendered_prompt_section import RenderedPromptSection
1212
from teams.ai.tokenizers import Tokenizer
1313
from teams.state.memory import Memory
1414

python/packages/ai/teams/ai/prompts/__init__.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@
44
"""
55

66
from .assistant_message import AssistantMessage
7-
from .conversation_history import ConversationHistory
8-
from .data_source_section import DataSourceSection
97
from .function_call import FunctionCall
108
from .function_call_message import FunctionCallMessage
119
from .function_response_message import FunctionResponseMessage
12-
from .group_section import GroupSection
13-
from .layout_engine import LayoutEngine
1410
from .message import ImageContentPart, ImageUrl, Message, TextContentPart
1511
from .prompt import Prompt
1612
from .prompt_functions import PromptFunction, PromptFunctions
1713
from .prompt_manager import PromptManager
1814
from .prompt_manager_options import PromptManagerOptions
19-
from .prompt_section import PromptSection
20-
from .prompt_section_base import PromptSectionBase
2115
from .prompt_template import PromptTemplate
2216
from .rendered_prompt_section import RenderedPromptSection
17+
from .sections import *
2318
from .system_message import SystemMessage
24-
from .template_section import TemplateSection
25-
from .text_section import TextSection
2619
from .user_input_message import UserInputMessage
2720
from .user_message import UserMessage

python/packages/ai/teams/ai/prompts/assistant_message.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Licensed under the MIT License.
44
"""
55

6-
from .template_section import TemplateSection
6+
from .sections.template_section import TemplateSection
77

88

99
class AssistantMessage(TemplateSection):

python/packages/ai/teams/ai/prompts/function_call_message.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from .function_call import FunctionCall
1515
from .message import Message
1616
from .prompt_functions import PromptFunctions
17-
from .prompt_section_base import PromptSectionBase
1817
from .rendered_prompt_section import RenderedPromptSection
18+
from .sections.prompt_section_base import PromptSectionBase
1919

2020

2121
class FunctionCallMessage(PromptSectionBase):

python/packages/ai/teams/ai/prompts/function_response_message.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from ..utilities import to_string
1313
from .message import Message
1414
from .prompt_functions import PromptFunctions
15-
from .prompt_section_base import PromptSectionBase
1615
from .rendered_prompt_section import RenderedPromptSection
16+
from .sections.prompt_section_base import PromptSectionBase
1717

1818

1919
class FunctionResponseMessage(PromptSectionBase):

python/packages/ai/teams/ai/prompts/prompt.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
from typing import List
77

8-
from .layout_engine import LayoutEngine
9-
from .prompt_section import PromptSection
8+
from .sections.layout_engine_section import LayoutEngineSection
9+
from .sections.prompt_section import PromptSection
1010

1111

12-
class Prompt(LayoutEngine):
12+
class Prompt(LayoutEngineSection):
1313
"""
1414
Top level prompt section.
1515

python/packages/ai/teams/ai/prompts/prompt_manager.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
from ...state import Memory
1616
from ..data_sources import DataSource
1717
from ..tokenizers import Tokenizer
18-
from .conversation_history import ConversationHistory
19-
from .group_section import GroupSection
2018
from .prompt import Prompt
2119
from .prompt_functions import PromptFunction, PromptFunctions
2220
from .prompt_manager_options import PromptManagerOptions
23-
from .prompt_section import PromptSection
2421
from .prompt_template import PromptTemplate
2522
from .prompt_template_config import PromptTemplateConfig
26-
from .template_section import TemplateSection
23+
from .sections import (
24+
ConversationHistorySection,
25+
GroupSection,
26+
PromptSection,
27+
TemplateSection,
28+
)
2729
from .user_input_message import UserInputMessage
2830
from .user_message import UserMessage
2931

@@ -254,7 +256,7 @@ async def get_prompt(self, name) -> PromptTemplate:
254256
# max_input_tokens.
255257
if template_config.completion.include_history:
256258
sections.append(
257-
ConversationHistory(
259+
ConversationHistorySection(
258260
f"conversation.{template_name}_history",
259261
self._options.max_conversation_history_tokens,
260262
)

python/packages/ai/teams/ai/prompts/prompt_section_layout.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from dataclasses import dataclass
77
from typing import Generic, Optional, TypeVar
88

9-
from .prompt_section import PromptSection
109
from .rendered_prompt_section import RenderedPromptSection
10+
from .sections.prompt_section import PromptSection
1111

1212
T = TypeVar("T")
1313

python/packages/ai/teams/ai/prompts/prompt_template.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
from dataclasses import dataclass
77

8-
from .prompt_section import PromptSection
98
from .prompt_template_config import PromptTemplateConfig
9+
from .sections.prompt_section import PromptSection
1010

1111

1212
@dataclass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Copyright (c) Microsoft Corporation. All rights reserved.
3+
Licensed under the MIT License.
4+
"""
5+
from .conversation_history_section import ConversationHistorySection
6+
from .data_source_section import DataSourceSection
7+
from .group_section import GroupSection
8+
from .layout_engine_section import LayoutEngineSection
9+
from .prompt_section import PromptSection
10+
from .prompt_section_base import PromptSectionBase
11+
from .template_section import TemplateSection
12+
from .text_section import TextSection

python/packages/ai/teams/ai/prompts/conversation_history.py renamed to python/packages/ai/teams/ai/prompts/sections/conversation_history_section.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
from botbuilder.core import TurnContext
99

10-
from ...state import Memory
11-
from ..tokenizers import Tokenizer
12-
from ..utilities import to_string
13-
from .message import Message
14-
from .prompt_functions import PromptFunctions
10+
from ....state import Memory
11+
from ...tokenizers import Tokenizer
12+
from ...utilities import to_string
13+
from ..message import Message
14+
from ..prompt_functions import PromptFunctions
15+
from ..rendered_prompt_section import RenderedPromptSection
1516
from .prompt_section_base import PromptSectionBase
16-
from .rendered_prompt_section import RenderedPromptSection
1717

1818

19-
class ConversationHistory(PromptSectionBase):
19+
class ConversationHistorySection(PromptSectionBase):
2020
"""
2121
A section that renders the conversation history.
2222
"""
@@ -109,7 +109,7 @@ async def render_as_text(
109109
for msg in reversed(history):
110110
msg = Message(role=msg.role, content=to_string(tokenizer, msg.content))
111111
prefix = self.user_prefix if msg.role == "user" else self.assistant_prefix
112-
line = prefix + msg.content
112+
line = prefix + (msg.content if msg.content is not None else "")
113113
length = len(tokenizer.encode(line)) + (separator_length if len(lines) > 0 else 0)
114114

115115
# Add initial line if required
@@ -169,7 +169,13 @@ async def render_as_messages(
169169

170170
# Add length of any image parts
171171
if isinstance(message.content, list):
172-
length += sum(1 for part in message.content if part.type == "image") * 85
172+
count = 0
173+
174+
for part in message.content:
175+
if not isinstance(part, str) and part.type == "image":
176+
count += 1
177+
178+
length += count * 85
173179

174180
# Add initial message if required
175181
if len(messages) == 0 and self.required:

python/packages/ai/teams/ai/prompts/data_source_section.py renamed to python/packages/ai/teams/ai/prompts/sections/data_source_section.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
from botbuilder.core import TurnContext
99

10-
from ...state import Memory
11-
from ..data_sources import DataSource
12-
from ..tokenizers import Tokenizer
13-
from .message import Message
14-
from .prompt_functions import PromptFunctions
10+
from ....state import Memory
11+
from ...data_sources import DataSource
12+
from ...tokenizers import Tokenizer
13+
from ..message import Message
14+
from ..prompt_functions import PromptFunctions
15+
from ..rendered_prompt_section import RenderedPromptSection
1516
from .prompt_section_base import PromptSectionBase
16-
from .rendered_prompt_section import RenderedPromptSection
1717

1818

1919
class DataSourceSection(PromptSectionBase):

python/packages/ai/teams/ai/prompts/group_section.py renamed to python/packages/ai/teams/ai/prompts/sections/group_section.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77

88
from botbuilder.core import TurnContext
99

10-
from ...state import Memory
11-
from ..tokenizers import Tokenizer
12-
from .layout_engine import LayoutEngine
13-
from .message import Message
14-
from .prompt_functions import PromptFunctions
10+
from ....state import Memory
11+
from ...tokenizers import Tokenizer
12+
from ..message import Message
13+
from ..prompt_functions import PromptFunctions
14+
from ..rendered_prompt_section import RenderedPromptSection
15+
from .layout_engine_section import LayoutEngineSection
1516
from .prompt_section import PromptSection
1617
from .prompt_section_base import PromptSectionBase
17-
from .rendered_prompt_section import RenderedPromptSection
1818

1919

2020
class GroupSection(PromptSectionBase):
2121
"""
2222
A group of sections that will rendered as a single message.
2323
"""
2424

25-
_layout_engine: LayoutEngine
25+
_layout_engine: LayoutEngineSection
2626
_sections: List[PromptSection]
2727
_role: str
2828

@@ -48,7 +48,7 @@ def __init__(
4848
text_prefix (str, optional): Prefix to use for text output. Defaults to ``.
4949
"""
5050
super().__init__(tokens, required, separator, text_prefix)
51-
self._layout_engine = LayoutEngine(sections, tokens, required, separator)
51+
self._layout_engine = LayoutEngineSection(sections, tokens, required, separator)
5252
self._sections = sections
5353
self._role = role
5454

python/packages/ai/teams/ai/prompts/layout_engine.py renamed to python/packages/ai/teams/ai/prompts/sections/layout_engine_section.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
from botbuilder.core import TurnContext
1010

11-
from ...state import Memory
12-
from ..tokenizers import Tokenizer
13-
from .message import Message
14-
from .prompt_functions import PromptFunctions
11+
from ....state import Memory
12+
from ...tokenizers import Tokenizer
13+
from ..message import Message
14+
from ..prompt_functions import PromptFunctions
15+
from ..prompt_section_layout import _PromptSectionLayout
16+
from ..rendered_prompt_section import RenderedPromptSection
1517
from .prompt_section import PromptSection
16-
from .prompt_section_layout import _PromptSectionLayout
17-
from .rendered_prompt_section import RenderedPromptSection
1818

1919

20-
class LayoutEngine(PromptSection):
20+
class LayoutEngineSection(PromptSection):
2121
"""
2222
Base layout engine that renders a set of `auto`, `fixed`, or `proportional` length sections.
2323
This class is used internally by the `Prompt` and `GroupSection` classes to
@@ -159,7 +159,7 @@ def _add_sections_to_layout(
159159
self, sections: List[PromptSection], layout: List[_PromptSectionLayout[Any]]
160160
):
161161
for section in sections:
162-
if isinstance(section, LayoutEngine):
162+
if isinstance(section, LayoutEngineSection):
163163
self._add_sections_to_layout(section.sections, layout)
164164
else:
165165
layout.append(_PromptSectionLayout(section=section))

python/packages/ai/teams/ai/prompts/prompt_section.py renamed to python/packages/ai/teams/ai/prompts/sections/prompt_section.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
from botbuilder.core import TurnContext
1010

11-
from ...state import Memory
12-
from ..tokenizers import Tokenizer
13-
from .message import Message
14-
from .prompt_functions import PromptFunctions
15-
from .rendered_prompt_section import RenderedPromptSection
11+
from ....state import Memory
12+
from ...tokenizers import Tokenizer
13+
from ..message import Message
14+
from ..prompt_functions import PromptFunctions
15+
from ..rendered_prompt_section import RenderedPromptSection
1616

1717

1818
class PromptSection(ABC):

python/packages/ai/teams/ai/prompts/prompt_section_base.py renamed to python/packages/ai/teams/ai/prompts/sections/prompt_section_base.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
from botbuilder.core import TurnContext
1212

13-
from ...state import Memory
14-
from ..tokenizers import Tokenizer
15-
from .message import Message, MessageContentParts, TextContentPart
16-
from .prompt_functions import PromptFunctions
13+
from ....state import Memory
14+
from ...tokenizers import Tokenizer
15+
from ..message import Message, MessageContentParts, TextContentPart
16+
from ..prompt_functions import PromptFunctions
17+
from ..rendered_prompt_section import RenderedPromptSection
1718
from .prompt_section import PromptSection
18-
from .rendered_prompt_section import RenderedPromptSection
1919

2020

2121
class PromptSectionBase(PromptSection):

0 commit comments

Comments
 (0)