123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- package model_entities
- import (
- "testing"
- "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
- )
- func TestFullFunctionPromptMessage(t *testing.T) {
- const (
- system_message = `
- {
- "role": "system",
- "content": "you are a helpful assistant"
- }
- `
- user_message = `
- {
- "role": "user",
- "content": "hello"
- }`
- assistant_message = `
- {
- "role": "assistant",
- "content": "you are a helpful assistant"
- }`
- image_message = `
- {
- "role": "user",
- "content": [
- {
- "type": "image",
- "data": "base64"
- }
- ]
- }`
- tool_message = `
- {
- "role": "tool",
- "content": "you are a helpful assistant",
- "tool_call_id": "123"
- }
- `
- )
- promptMessage, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(system_message))
- if err != nil {
- t.Error(err)
- }
- if promptMessage.Role != "system" {
- t.Error("role is not system")
- }
- promptMessage, err = parser.UnmarshalJsonBytes[PromptMessage]([]byte(user_message))
- if err != nil {
- t.Error(err)
- }
- if promptMessage.Role != "user" {
- t.Error("role is not user")
- }
- promptMessage, err = parser.UnmarshalJsonBytes[PromptMessage]([]byte(assistant_message))
- if err != nil {
- t.Error(err)
- }
- if promptMessage.Role != "assistant" {
- t.Error("role is not assistant")
- }
- promptMessage, err = parser.UnmarshalJsonBytes[PromptMessage]([]byte(image_message))
- if err != nil {
- t.Error(err)
- }
- if promptMessage.Role != "user" {
- t.Error("role is not user")
- }
- if promptMessage.Content.([]PromptMessageContent)[0].Type != "image" {
- t.Error("type is not image")
- }
- promptMessage, err = parser.UnmarshalJsonBytes[PromptMessage]([]byte(tool_message))
- if err != nil {
- t.Error(err)
- }
- if promptMessage.Role != "tool" {
- t.Error("role is not tool")
- }
- if promptMessage.ToolCallId != "123" {
- t.Error("tool call id is not 123")
- }
- }
- func TestWrongRole(t *testing.T) {
- const (
- wrong_role = `
- {
- "role": "wrong",
- "content": "you are a helpful assistant"
- }
- `
- )
- _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(wrong_role))
- if err == nil {
- t.Error("error is nil")
- }
- }
- func TestWrongContent(t *testing.T) {
- const (
- wrong_content = `
- {
- "role": "user",
- "content": 123
- }
- `
- )
- _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(wrong_content))
- if err == nil {
- t.Error("error is nil")
- }
- }
- func TestWrongContentArray(t *testing.T) {
- const (
- wrong_content_array = `
- {
- "role": "user",
- "content": [
- {
- "type": "image",
- "data": 123
- }
- ]
- }
- `
- )
- _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(wrong_content_array))
- if err == nil {
- t.Error("error is nil")
- }
- }
- // func TestWrongContentArray2(t *testing.T) {
- // const (
- // wrong_content_array2 = `
- // {
- // "role": "user",
- // "content": [
- // {
- // "type": "image"
- // }
- // ]
- // }
- // `
- // )
- // _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(wrong_content_array2))
- // if err == nil {
- // t.Error("error is nil")
- // }
- // }
- func TestWrongContentArray3(t *testing.T) {
- const (
- wrong_content_array3 = `
- {
- "role": "user",
- "content": [
- {
- "type": "wwww",
- "data": "base64"
- },
- {
- "type": "image",
- "data": "base64"
- }
- ]
- }
- `
- )
- _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(wrong_content_array3))
- if err == nil {
- t.Error("error is nil")
- }
- }
- func TestFullFunctionLLMResultChunk(t *testing.T) {
- const (
- llm_result_chunk = `
- {
- "model": "gpt-3.5-turbo",
- "prompt_messages": [
- {
- "role": "system",
- "content": "you are a helpful assistant"
- },
- {
- "role": "user",
- "content": "hello"
- }
- ],
- "system_fingerprint": "123",
- "delta": {
- "index" : 0,
- "message" : {
- "role": "assistant",
- "content": "I am a helpful assistant"
- },
- "usage": {
- "prompt_tokens": 10,
- "prompt_unit_price": 0.1,
- "prompt_price_unit": 1,
- "prompt_price": 1,
- "completion_tokens": 10,
- "completion_unit_price": 0.1,
- "completion_price_unit": 1,
- "completion_price": 1,
- "total_tokens": 20,
- "total_price": 2,
- "currency": "usd",
- "latency": 0.1
- },
- "finish_reason": "completed"
- }
- }
- `
- )
- _, err := parser.UnmarshalJsonBytes[LLMResultChunk]([]byte(llm_result_chunk))
- if err != nil {
- t.Error(err)
- }
- }
- func TestZeroLLMUsage(t *testing.T) {
- const (
- llm_usage = `
- {
- "prompt_tokens": 0,
- "prompt_unit_price": 0,
- "prompt_price_unit": 0,
- "prompt_price": 0,
- "completion_tokens": 0,
- "completion_unit_price": 0,
- "completion_price_unit": 0,
- "completion_price": 0,
- "total_tokens": 0,
- "total_price": 0,
- "currency": "usd",
- "latency": 0
- }
- `
- )
- _, err := parser.UnmarshalJsonBytes[LLMUsage]([]byte(llm_usage))
- if err != nil {
- t.Error(err)
- }
- }
- func TestTextPromptMessage(t *testing.T) {
- const (
- promptMessage = `
- {
- "role": "user",
- "content": "hello"
- }
- `
- )
- _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(promptMessage))
- if err != nil {
- t.Error(err)
- }
- }
- func TestImagePromptMessage(t *testing.T) {
- const (
- promptMessage = `
- {
- "role": "user",
- "content": [
- {
- "type": "image",
- "data": "base64"
- }
- ]
- }
- `
- )
- _, err := parser.UnmarshalJsonBytes[PromptMessage]([]byte(promptMessage))
- if err != nil {
- t.Error(err)
- }
- }
|