Inline Attribution Formatter

Chat answers contain inline attributions and uses DefaultFormatter by default.

Example:

# Create a new chat
chat = bigdata.chat.new("Pfizer company analysis", formatter=DefaultFormatter())

# Ask a question
response = chat.ask("Evaluate the experience and reputation of the management team of Pfizer")

Output:

## Pfizer's Management Team

### CEO and Chairman: Dr. Albert Bourla
- Dr. Bourla has been the Chairman and CEO of Pfizer since 2019 `:ref[13]`
- He is a Doctor of Veterinary Medicine and holds a Ph.D. from the Veterinary School of Aristotle University `:ref[13]`
- In 2022, he was named the Genesis Prize Laureate, often referred to as the "Jewish Nobel Prize" `:ref[13]`
...

DefaultFormatter adds inline attributes as a `:ref[index]` where index is an index of inline attribution element in sources property in ChatInteraction

There are two formatters available at the moment and it is also possible to Create a custom formatter

  1. DefaultFormatter

  2. MarkdownLinkFormatter

How to configure it

You can configure the inline attribution formatter at different levels:

  • When chat is created

  • When getting existing chat

  • When getting list of chats

  • When making chat.ask request

When chat is created

from bigdata_client.models.chat import MarkdownLinkFormatter

chat = bigdata.chat.new("Pfizer company analysis", formatter=MarkdownLinkFormatter())

# all following chat.ask calls will use formatter we set when creating chat
response = chat.ask("Evaluate the experience and reputation of the management team of Pfizer")
print(response.answer)

When getting existing chat

from bigdata_client.models.chat import MarkdownLinkFormatter

# Get existing chat
chat_id = chat.id

chat = bigdata.chat.get(chat_id, formatter=MarkdownLinkFormatter())

# All chat interactions will be formatted using provided formatter
for interaction in chat.interactions:
    # answer will be formatted with formatter
    print(interaction.answer)

# All following chat.ask calls will use formatter we set when fetching chat
response = chat.ask("Evaluate the experience and reputation of the management team of Pfizer")
print(response.answer)

When getting list of available chats

from bigdata_client.models.chat import MarkdownLinkFormatter

# Get list of chats
chats = bigdata.chat.list(formatter=MarkdownLinkFormatter())

# All chat interactions will be formatted using provided formatter, let's focus only in our first chat
for interaction in chats[0].interactions:
    # answer will be formatted with formatter
    print(interaction.answer)

# All following chat.ask calls will use formatter we set when fetching chat list

When making chat.ask request

In case it’s necessary to chage formatter only for one chat.ask call there is a possibility:

# Create a new chat
chat = bigdata.chat.new("Pfizer company analysis")

# This specific call to chat.ask will use MarkdownLinkFormatter
response = chat.ask(
    "Evaluate the experience and reputation of the management team of Pfizer",
    formatter=MarkdownLinkFormatter()
)
print(response.answer)

# All following chat.ask calls will use formatter we set when creating chat
response = chat.ask("Evaluate the experience and reputation of the management team of Pfizer")
print(response.answer)

Create a custom formatter

It is possible to create custom inline attribution formatter.

All inline attribution formatters extend abstract class InlineAttributionFormatter

It is necessary to implement format method.

Here an example how to create custom formatter:

from bigdata_client.models.chat import InlineAttributionFormatter, ChatSource

class HtmlLinkFormatter(InlineAttributionFormatter):
    def format(self, index: int, attribution: ChatSource) -> str:
        a_text = attribution.headline
        a_href = attribution.url
        return f'<a href="{a_href}">{a_text}</a>'

# Create a new chat using custom formatter
chat = bigdata.chat.new("Pfizer company analysis", formatter=HtmlLinkFormatter())
response = chat.ask("Evaluate the experience and reputation of the management team of Pfizer")
print(response.answer)

Output:

## Pfizer's Management Team

Pfizer's management team is led by a highly experienced and respected group of executives:

### Albert Bourla, Chairman and Chief Executive Officer
- Joined Pfizer in 1993 and has held various leadership roles, including Group President of Pfizer's Innovative Health business <a href="https://www.linkedin.com/in/albert-bourla">Albert Bourla - Chief Executive Officer - Pfizer</a>
- Became CEO in 2019 and was named the Genesis Prize Laureate in 2022 <a href="https://www.linkedin.com/in/albert-bourla">Albert Bourla - Chief Executive Officer - Pfizer</a>