Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ai statistics #1303

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Conversation

rinfx
Copy link
Collaborator

@rinfx rinfx commented Sep 12, 2024

增加用户扩展 log & span attribute 机制

@codecov-commenter
Copy link

codecov-commenter commented Sep 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 44.23%. Comparing base (ef31e09) to head (9e48586).
Report is 111 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1303      +/-   ##
==========================================
+ Coverage   35.91%   44.23%   +8.32%     
==========================================
  Files          69       76       +7     
  Lines       11576     9895    -1681     
==========================================
+ Hits         4157     4377     +220     
+ Misses       7104     5183    -1921     
- Partials      315      335      +20     

see 81 files with indirect coverage changes

@johnlanni johnlanni marked this pull request as draft September 13, 2024 01:46
plugins/wasm-go/extensions/ai-statistics/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-statistics/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-statistics/main.go Outdated Show resolved Hide resolved
logAttributes[logAttribute.Key] = value
}
case "request_body":
raw := gjson.GetBytes(body, logAttribute.Value).Raw
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥不用 .String()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raw取的是序列化后的数据,用.String()的话,打印日志的时候会有格式问题

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

具体是什么格式问题呢,能举个例子吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{"ai_log":"{"consumer":"21321r9fncsb2dq","question":"用三行python实现求和函数","answer":"在Python中,你可以使用内置的sum()函数来计算一个数字列表的总和。这是一个简单的实现:\n\npython\\ndef sum_function(numbers):\\n return sum(numbers)\\n\\n# 测试函数\\nnumbers = [1, 2, 3, 4, 5]\\nprint(sum_function(numbers)) # 输出:15\\n\n\n如果你想要自己手动实现一个求和函数,可以使用以下代码:\n\npython\\ndef sum_function(numbers):\\n total = 0\\n for number in numbers:\\n total += number\\n return total\\n\\n# 测试函数\\nnumbers = [1, 2, 3, 4, 5]\\nprint(sum_function(numbers)) # 输出:15\\n\n\n在这两个例子中,我们定义了一个名为sum_function的函数,它接受一个数字列表作为参数,并返回该列表中所有数字的总和。","output_token":"188","llm_first_token_duration":"237","llm_service_duration":"4341","model":"qwen-turbo","input_token":"16"}"}

Copy link
Collaborator Author

@rinfx rinfx Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面这个是一个例子,如果LLM返回的内容中有换行符之类的,容易有问题,我直接复制粘贴被github打印了。。原始的是这样的:
image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{"ai_log":"{\"consumer\":\"21321r9fncsb2dq\",\"question\":\"用三行python实现求和函数\",\"answer\":\"在Python中,你可以使用内置的`sum()`函数来计算一个数字列表的总和。这是一个简单的实现:\\n\\n```python\\ndef sum_function(numbers):\\n    return sum(numbers)\\n\\n# 测试函数\\nnumbers = [1, 2, 3, 4, 5]\\nprint(sum_function(numbers))  # 输出:15\\n```\\n\\n如果你想要自己手动实现一个求和函数,可以使用以下代码:\\n\\n```python\\ndef sum_function(numbers):\\n    total = 0\\n    for number in numbers:\\n        total += number\\n    return total\\n\\n# 测试函数\\nnumbers = [1, 2, 3, 4, 5]\\nprint(sum_function(numbers))  # 输出:15\\n```\\n\\n在这两个例子中,我们定义了一个名为`sum_function`的函数,它接受一个数字列表作为参数,并返回该列表中所有数字的总和。\",\"output_token\":\"188\",\"llm_first_token_duration\":\"237\",\"llm_service_duration\":\"4341\",\"model\":\"qwen-turbo\",\"input_token\":\"16\"}"}

return data
}
setTraceAttributeValueBySource(config, "response_streaming_body", streamingBodyBuffer, log)
setLogAttributeValueBySource(ctx, config, "response_streaming_body", streamingBodyBuffer, log)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么 endOfStream 的时候记录的 attribute 还是 “response_streaming_body” source 的?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

流式和非流式对响应的处理是有区别的

plugins/wasm-go/extensions/ai-statistics/README.md Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-statistics/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-statistics/main.go Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants