Skip to content

Commit

Permalink
[repo] fix: max_tokens bug fix in completions (#1887)
Browse files Browse the repository at this point in the history
## Linked issues

closes: #1835, #1800

## Details

- `max_input_tokens` was incorrectly passed in as openai's `max_token`
(for both C# and Python)

## Attestation Checklist

- [x] My code follows the style guidelines of this project

- I have checked for/fixed spelling, linting, and other errors
- I have commented my code for clarity
- I have made corresponding changes to the documentation (updating the
doc strings in the code is sufficient)
- My changes generate no new warnings
- I have added tests that validates my changes, and provides sufficient
test coverage. I have tested with:
  - Local testing
  - E2E testing in Teams
- New and existing unit tests pass locally with my changes
  • Loading branch information
lilyydu authored Aug 1, 2024
1 parent a1d4008 commit 79a006f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public async Task<PromptResponse> CompletePromptAsync(ITurnContext turnContext,
IEnumerable<OAIChat.ChatMessage> chatMessages = prompt.Output.Select(chatMessage => chatMessage.ToOpenAIChatMessage());

ChatCompletionOptions? chatCompletionOptions = ModelReaderWriter.Read<ChatCompletionOptions>(BinaryData.FromString($@"{{
""max_tokens"": {maxInputTokens},
""max_tokens"": {promptTemplate.Configuration.Completion.MaxTokens},
""temperature"": {(float)promptTemplate.Configuration.Completion.Temperature},
""top_p"": {(float)promptTemplate.Configuration.Completion.TopP},
""presence_penalty"": {(float)promptTemplate.Configuration.Completion.PresencePenalty},
Expand Down
8 changes: 4 additions & 4 deletions python/packages/ai/teams/ai/models/openai_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def complete_prompt(
tokenizer: Tokenizer,
template: PromptTemplate,
) -> PromptResponse[str]:
max_tokens = template.config.completion.max_input_tokens
max_input_tokens = template.config.completion.max_input_tokens
model = (
template.config.completion.model
if template.config.completion.model is not None
Expand All @@ -134,15 +134,15 @@ async def complete_prompt(
memory=memory,
functions=functions,
tokenizer=tokenizer,
max_tokens=max_tokens,
max_tokens=max_input_tokens,
)

if res.too_long:
return PromptResponse[str](
status="too_long",
error=f"""
the generated chat completion prompt had a length of {res.length} tokens
which exceeded the max_input_tokens of {max_tokens}
which exceeded the max_input_tokens of {max_input_tokens}
""",
)

Expand Down Expand Up @@ -194,7 +194,7 @@ async def complete_prompt(
frequency_penalty=template.config.completion.frequency_penalty,
top_p=template.config.completion.top_p,
temperature=template.config.completion.temperature,
max_tokens=max_tokens,
max_tokens=template.config.completion.max_tokens,
extra_body=extra_body,
)

Expand Down
7 changes: 4 additions & 3 deletions python/packages/ai/teams/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ async def __handler__(context: TurnContext, state: StateT):
return False

feedback = context.activity.value
feedback.reply_to_id=context.activity.reply_to_id
feedback.reply_to_id = context.activity.reply_to_id

await func(context, state, feedback)
await context.send_activity(
Expand Down Expand Up @@ -819,8 +819,9 @@ async def _run_ai_chain(self, context: TurnContext, state):
return True

def _contains_non_text_attachments(self, context):
non_text_attachments = filter(lambda a: not a.content_type.startswith(
"text/html"), context.activity.attachments)
non_text_attachments = filter(
lambda a: not a.content_type.startswith("text/html"), context.activity.attachments
)
return len(list(non_text_attachments)) > 0

async def _run_after_turn_middleware(self, context: TurnContext, state):
Expand Down

0 comments on commit 79a006f

Please sign in to comment.