newrelic / newrelic-python-agent

New Relic Python Agent
https://docs.newrelic.com/docs/agents/python-agent
Apache License 2.0
176 stars 99 forks source link

Fix openai streaming wrong span bug #1110

Closed hmstepanek closed 6 months ago

hmstepanek commented 6 months ago

Overview

To reproduce:

import newrelic.agent

@newrelic.agent.background_task()
def streamingOpenAI(prompt):
    import openai

    print(f'prompt with openai: {prompt}')

    response = openai.chat.completions.create(
        model='gpt-3.5-turbo',
        messages=[
            {'role': 'user', 'content': prompt}
        ],
        temperature=0,
        stream=True,
    )

    message = ""
    i = 1
    for chunk in response:
        if 'choices' in chunk:
            i+=1
            for choice in chunk['choices']:
                if 'delta' in choice and 'content' in choice['delta']:
                    message += choice['delta']['content']

    print(f'output ({i}): {message}\n\n')

if __name__ == "__main__":
    # Enable New Relic Python agent
    newrelic.agent.initialize('newrelic.ini')
    newrelic.agent.register_application(timeout=10)

    prompt = "What is 2 + 2?"

    streamingOpenAI(prompt)
    # Allow the New Relic agent to send final messages as part of shutdown
    # The agent by default can send data up to a minute later
    newrelic.agent.shutdown_agent(60)

    print("Agent run finished!")

When you click on the AI details view of the openai span openai create, previously the details would error and not show up. After this fix they do.

github-actions[bot] commented 6 months ago

🦙 MegaLinter status: ❌ ERROR

Descriptor Linter Files Fixed Errors Elapsed time
❌ PYTHON bandit 19 1 10.93s
✅ PYTHON black 71 6 0 8.71s
❌ PYTHON flake8 71 9 4.4s
✅ PYTHON isort 71 9 0 0.52s
❌ PYTHON pylint 71 152 33.99s

See detailed report in MegaLinter reports _Set VALIDATE_ALL_CODEBASE: true in mega-linter.yml to validate all sources, not only the diff_

_MegaLinter is graciously provided by OX Security_