|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/cloudwego/eino-ext/components/model/claude" |
| 10 | + "github.com/cloudwego/eino/schema" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + systemCache() |
| 15 | + //toolInfoCache() |
| 16 | +} |
| 17 | + |
| 18 | +func systemCache() { |
| 19 | + apiKey := os.Getenv("CLAUDE_API_KEY") |
| 20 | + modelName := os.Getenv("CLAUDE_MODEL") |
| 21 | + baseURL := os.Getenv("CLAUDE_BASE_URL") |
| 22 | + |
| 23 | + ctx := context.Background() |
| 24 | + |
| 25 | + cm, err := claude.NewChatModel(ctx, &claude.Config{ |
| 26 | + // if you want to use Aws Bedrock Service, set these four field. |
| 27 | + // ByBedrock: true, |
| 28 | + // AccessKey: "", |
| 29 | + // SecretAccessKey: "", |
| 30 | + // Region: "us-west-2", |
| 31 | + APIKey: apiKey, |
| 32 | + // Model: "claude-3-5-sonnet-20240620", |
| 33 | + BaseURL: &baseURL, |
| 34 | + Model: modelName, |
| 35 | + MaxTokens: 3000, |
| 36 | + }) |
| 37 | + if err != nil { |
| 38 | + log.Fatalf("NewChatModel of claude failed, err=%v", err) |
| 39 | + } |
| 40 | + |
| 41 | + breakpoint := claude.SetMessageBreakpoint(&schema.Message{ |
| 42 | + Role: schema.System, |
| 43 | + Content: "The film Dongji Rescue, based on a true historical event, tells the story of Chinese fishermen braving turbulent seas to save strangers — a testament to the nation's capacity to create miracles in the face of adversity.\n\nEighty-three years ago, in 1942, the Japanese military seized the Lisbon Maru ship to transport 1,816 British prisoners of war from Hong Kong to Japan. Passing through the waters near Zhoushan, Zhejiang province, it was torpedoed by a US submarine. Fishermen from Zhoushan rescued 384 prisoners of war and hid them from Japanese search parties.\n\nActor Zhu Yilong, in an exclusive interview with China Daily, said he was deeply moved by the humanity shown in such extreme conditions when he accepted his role. \"This historical event proves that in dire circumstances, Chinese people can extend their goodness to protect both themselves and others,\" he said.\n\nLast Friday, a themed event for Dongji Rescue was held in Zhoushan, a city close to where the Lisbon Maru sank. Descendants of the British POWs and the rescuing fishermen gathered to watch the film and share their reflections.\n\nIn the film, the British POWs are aided by a group of fishermen from Dongji Island, whose courage and compassion cut a path through treacherous waves. After the screening, many descendants were visibly moved.\n\n\"I want to express my deepest gratitude to the Chinese people and the descendants of Chinese fishermen. When the film is released in the UK, I will bring my family and friends to watch it. Heroic acts like this deserve to be known worldwide,\" said Denise Wynne, a descendant of a British POW.\n\n\"I felt the profound friendship between the Chinese and British people through the film,\" said Li Hui, a descendant of a rescuer. Many audience members were brought to tears — some by the fishermen's bravery, others by the enduring spirit of \"never abandoning those in peril\".\n\n\"In times of sea peril, rescue is a must,\" said Wu Buwei, another rescuer's descendant, noting that this value has long been a part of Dongji fishermen's traditions.\n\nCoincidentally, on the morning of Aug 5, a real-life rescue unfolded on the shores of Dongfushan in Dongji town. Two tourists from Shanghai fell into the sea and were swept away by powerful waves. Without hesitation, two descendants of Dongji fishermen — Wang Yubin and Yang Xiaoping — leaped in to save them, braving a wind force of 9 to 10 before pulling them to safety.\n\n\"Our forebearers once rescued many British POWs here. As their descendants, we should carry forward their bravery,\" Wang said. Both rescuers later joined the film's cast and crew at the Zhoushan event, sharing the stage with actors who portrayed the fishermen in a symbolic \"reunion across 83 years\".\n\nChinese actor Wu Lei, who plays A Dang in the film, expressed his respect for the two rescuing fishermen on-site. In the film, A Dang saves a British POW named Thomas Newman. Though they share no common language, they manage to connect. In an interview with China Daily, Wu said, \"They connected through a small globe. A Dang understood when Newman pointed out his home, the UK, on the globe.\"\n\nThe film's director Fei Zhenxiang noted that the Eastern Front in World War II is often overlooked internationally, despite China's 14 years of resistance tying down large numbers of Japanese troops. \"Every Chinese person is a hero, and their righteousness should not be forgotten,\" he said.\n\nGuan Hu, codirector, said, \"We hope that through this film, silent kindness can be seen by the world\", marking the 80th anniversary of victory in the Chinese People's War of Resistance Against Japanese Aggression (1931-45) and the World Anti-Fascist War.", |
| 44 | + }) |
| 45 | + |
| 46 | + for i := 0; i < 2; i++ { |
| 47 | + now := time.Now() |
| 48 | + |
| 49 | + resp, err := cm.Generate(ctx, []*schema.Message{ |
| 50 | + { |
| 51 | + Role: schema.System, |
| 52 | + Content: "You are an AI assistant tasked with analyzing literary works. " + |
| 53 | + "Your goal is to provide insightful commentary on themes.", |
| 54 | + }, |
| 55 | + breakpoint, |
| 56 | + { |
| 57 | + Role: schema.User, |
| 58 | + Content: "Analyze the major themes in the content.", |
| 59 | + }, |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + log.Fatalf("Generate failed, err=%v", err) |
| 63 | + } |
| 64 | + |
| 65 | + log.Printf("time_consume=%f, output: \n%v", time.Now().Sub(now).Seconds(), resp) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func toolInfoCache() { |
| 70 | + apiKey := os.Getenv("CLAUDE_API_KEY") |
| 71 | + modelName := os.Getenv("CLAUDE_MODEL") |
| 72 | + baseURL := os.Getenv("CLAUDE_BASE_URL") |
| 73 | + |
| 74 | + ctx := context.Background() |
| 75 | + |
| 76 | + cm, err := claude.NewChatModel(ctx, &claude.Config{ |
| 77 | + // if you want to use Aws Bedrock Service, set these four field. |
| 78 | + // ByBedrock: true, |
| 79 | + // AccessKey: "", |
| 80 | + // SecretAccessKey: "", |
| 81 | + // Region: "us-west-2", |
| 82 | + APIKey: apiKey, |
| 83 | + // Model: "claude-3-5-sonnet-20240620", |
| 84 | + BaseURL: &baseURL, |
| 85 | + Model: modelName, |
| 86 | + MaxTokens: 3000, |
| 87 | + }) |
| 88 | + if err != nil { |
| 89 | + log.Fatalf("NewChatModel of claude failed, err=%v", err) |
| 90 | + } |
| 91 | + |
| 92 | + breakpoint := claude.SetToolInfoBreakpoint(&schema.ToolInfo{ |
| 93 | + Name: "get_time", |
| 94 | + Desc: "Get the current time in a given time zone", |
| 95 | + ParamsOneOf: schema.NewParamsOneOfByParams( |
| 96 | + map[string]*schema.ParameterInfo{ |
| 97 | + "timezone": { |
| 98 | + Required: true, |
| 99 | + Type: "string", |
| 100 | + Desc: "The IANA time zone name, e.g. America/Los_Angeles", |
| 101 | + }, |
| 102 | + }, |
| 103 | + )}, |
| 104 | + ) |
| 105 | + |
| 106 | + mockTools := []*schema.ToolInfo{ |
| 107 | + { |
| 108 | + Name: "get_weather", |
| 109 | + Desc: "Get the current weather in a given location", |
| 110 | + ParamsOneOf: schema.NewParamsOneOfByParams( |
| 111 | + map[string]*schema.ParameterInfo{ |
| 112 | + "location": { |
| 113 | + Type: "string", |
| 114 | + Desc: "The city and state, e.g. San Francisco, CA", |
| 115 | + }, |
| 116 | + "unit": { |
| 117 | + Type: "string", |
| 118 | + Enum: []string{"celsius", "fahrenheit"}, |
| 119 | + Desc: "The unit of temperature, either celsius or fahrenheit", |
| 120 | + }, |
| 121 | + }, |
| 122 | + ), |
| 123 | + }, |
| 124 | + { |
| 125 | + Name: "mock_tool_1", |
| 126 | + Desc: "The film Dongji Rescue, based on a true historical event, tells the story of Chinese fishermen braving turbulent seas to save strangers — a testament to the nation's capacity to create miracles in the face of adversity.\n\nEighty-three years ago, in 1942, the Japanese military seized the Lisbon Maru ship to transport 1,816 British prisoners of war from Hong Kong to Japan. Passing through the waters near Zhoushan, Zhejiang province, it was torpedoed by a US submarine. Fishermen from Zhoushan rescued 384 prisoners of war and hid them from Japanese search parties.\n\nActor Zhu Yilong, in an exclusive interview with China Daily, said he was deeply moved by the humanity shown in such extreme conditions when he accepted his role. \"This historical event proves that in dire circumstances, Chinese people can extend their goodness to protect both themselves and others,\" he said.\n\nLast Friday, a themed event for Dongji Rescue was held in Zhoushan, a city close to where the Lisbon Maru sank. Descendants of the British POWs and the rescuing fishermen gathered to watch the film and share their reflections.\n\nIn the film, the British POWs are aided by a group of fishermen from Dongji Island, whose courage and compassion cut a path through treacherous waves. After the screening, many descendants were visibly moved.\n\n\"I want to express my deepest gratitude to the Chinese people and the descendants of Chinese fishermen. When the film is released in the UK, I will bring my family and friends to watch it. Heroic acts like this deserve to be known worldwide,\" said Denise Wynne, a descendant of a British POW.\n\n\"I felt the profound friendship between the Chinese and British people through the film,\" said Li Hui, a descendant of a rescuer. Many audience members were brought to tears — some by the fishermen's bravery, others by the enduring spirit of \"never abandoning those in peril\".\n\n\"In times of sea peril, rescue is a must,\" said Wu Buwei, another rescuer's descendant, noting that this value has long been a part of Dongji fishermen's traditions.\n\nCoincidentally, on the morning of Aug 5, a real-life rescue unfolded on the shores of Dongfushan in Dongji town. Two tourists from Shanghai fell into the sea and were swept away by powerful waves. Without hesitation, two descendants of Dongji fishermen — Wang Yubin and Yang Xiaoping — leaped in to save them, braving a wind force of 9 to 10 before pulling them to safety.\n\n\"Our forebearers once rescued many British POWs here. As their descendants, we should carry forward their bravery,\" Wang said. Both rescuers later joined the film's cast and crew at the Zhoushan event, sharing the stage with actors who portrayed the fishermen in a symbolic \"reunion across 83 years\".\n\nChinese actor Wu Lei, who plays A Dang in the film, expressed his respect for the two rescuing fishermen on-site. In the film, A Dang saves a British POW named Thomas Newman. Though they share no common language, they manage to connect. In an interview with China Daily, Wu said, \"They connected through a small globe. A Dang understood when Newman pointed out his home, the UK, on the globe.\"\n\nThe film's director Fei Zhenxiang noted that the Eastern Front in World War II is often overlooked internationally, despite China's 14 years of resistance tying down large numbers of Japanese troops. \"Every Chinese person is a hero, and their righteousness should not be forgotten,\" he said.\n\nGuan Hu, codirector, said, \"We hope that through this film, silent kindness can be seen by the world\", marking the 80th anniversary of victory in the Chinese People's War of Resistance Against Japanese Aggression (1931-45) and the World Anti-Fascist War.", |
| 127 | + ParamsOneOf: schema.NewParamsOneOfByParams( |
| 128 | + map[string]*schema.ParameterInfo{ |
| 129 | + "mock_param": { |
| 130 | + Required: true, |
| 131 | + Type: "string", |
| 132 | + Desc: "mock parameter", |
| 133 | + }, |
| 134 | + }, |
| 135 | + ), |
| 136 | + }, |
| 137 | + { |
| 138 | + Name: "mock_tool_2", |
| 139 | + Desc: "For the administration, the biggest gain was Japan's commitment to investing $550 billion in the United States, particularly with funds to be directed to sectors deemed strategic, ranging from semiconductors and pharmaceuticals to energy infrastructure and critical minerals.The White House highlighted that the deal represents what it called the \"single largest foreign investment commitment ever secured by any country.\" It touted the agreement as a \"strategic realignment of the U.S.-Japan economic relationship\" that will advance the mutual interests of both countries.", |
| 140 | + ParamsOneOf: schema.NewParamsOneOfByParams( |
| 141 | + map[string]*schema.ParameterInfo{ |
| 142 | + "mock_param": { |
| 143 | + Required: true, |
| 144 | + Type: "string", |
| 145 | + Desc: "Bessent said it will check the extent of Japan's compliance every quarter and \"if the president is unhappy, then they will boomerang back to the 25 percent tariff rates, both on cars and the rest of their products.\"", |
| 146 | + }, |
| 147 | + }, |
| 148 | + ), |
| 149 | + }, |
| 150 | + { |
| 151 | + Name: "mock_tool_3", |
| 152 | + Desc: "Of the amount, up to 100,000 tons is imported for direct consumption. Most of the remainder is used for animal feed and processing into other food products. Any rice imported to Japan beyond the special quota is subject to a tariff of 341 yen ($2.3) per kilogram.\n\n\n In fiscal 2024, the United States shipped roughly 346,000 tons of rice to Japan, compared with 286,000 tons exported by Thailand and 70,000 tons by Australia, under the quota system, according to Japan's farm ministry.", |
| 153 | + ParamsOneOf: schema.NewParamsOneOfByParams( |
| 154 | + map[string]*schema.ParameterInfo{ |
| 155 | + "mock_param": { |
| 156 | + Required: true, |
| 157 | + Type: "string", |
| 158 | + Desc: "Currently, Japan imports about 770,000 tons of tariff-free rice a year under the WTO framework, with the United States the No. 1 exporter, accounting for nearly half of the total, followed by Thailand and Australia.", |
| 159 | + }, |
| 160 | + }, |
| 161 | + ), |
| 162 | + }, |
| 163 | + breakpoint, |
| 164 | + } |
| 165 | + |
| 166 | + chatModelWithTools, err := cm.WithTools(mockTools) |
| 167 | + if err != nil { |
| 168 | + log.Fatalf("WithTools failed, err=%v", err) |
| 169 | + } |
| 170 | + |
| 171 | + for i := 0; i < 2; i++ { |
| 172 | + now := time.Now() |
| 173 | + |
| 174 | + resp, err := chatModelWithTools.Generate(ctx, []*schema.Message{ |
| 175 | + { |
| 176 | + Role: schema.System, |
| 177 | + Content: "You are a tool calling assistant.", |
| 178 | + }, |
| 179 | + { |
| 180 | + Role: schema.User, |
| 181 | + Content: "Mock the get_weather tool input yourself, and then call get_weather", |
| 182 | + }, |
| 183 | + }) |
| 184 | + if err != nil { |
| 185 | + log.Fatalf("Generate failed, err=%v", err) |
| 186 | + } |
| 187 | + |
| 188 | + log.Printf("time_consume=%f, output: \n%v", time.Now().Sub(now).Seconds(), resp) |
| 189 | + } |
| 190 | +} |
0 commit comments