当前位置:   article > 正文

chatgpt升级啦,训练数据时间更新到2023年4月,支持tools(升级functionCall),128k上下文_chatgpt 4 时间

chatgpt 4 时间

 (2023年11月7日)

gpt-4-1106-preview

https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo

训练数据日期升级到2023年四月

上线文增加到128k

调用一次chatgpt接口,可以得到多次函数调用

 

  1. import OpenAI from "openai";
  2. const openai = new OpenAI();
  3. // Example dummy function hard coded to return the same weather
  4. // In production, this could be your backend API or an external API
  5. function getCurrentWeather(location, unit = "fahrenheit") {
  6. if (location.toLowerCase().includes("tokyo")) {
  7. return JSON.stringify({ location, temperature: "10", unit: "celsius" });
  8. } else if (location.toLowerCase().includes("san francisco")) {
  9. return JSON.stringify({ location, temperature: "72", unit: "fahrenheit" });
  10. } else {
  11. return JSON.stringify({ location, temperature: "22", unit: "celsius" });
  12. }
  13. }
  14. async function runConversation() {
  15. // Step 1: send the conversation and available functions to the model
  16. const messages = [
  17. { role: "user", content: "What's the weather like in San Francisco, Tokyo, and Paris?" },
  18. ];
  19. const tools = [
  20. {
  21. type: "function",
  22. function: {
  23. name: "get_current_weather",
  24. description: "Get the current weather in a given location",
  25. parameters: {
  26. type: "object",
  27. properties: {
  28. location: {
  29. type: "string",
  30. description: "The city and state, e.g. San Francisco, CA",
  31. },
  32. unit: { type: "string", enum: ["celsius", "fahrenheit"] },
  33. },
  34. required: ["location"],
  35. },
  36. },
  37. },
  38. ];
  39. const response = await openai.chat.completions.create({
  40. model: "gpt-3.5-turbo-1106",
  41. messages: messages,
  42. tools: tools,
  43. tool_choice: "auto", // auto is default, but we'll be explicit
  44. });
  45. const responseMessage = response.choices[0].message;
  46. // Step 2: check if the model wanted to call a function
  47. const toolCalls = responseMessage.tool_calls;
  48. if (responseMessage.tool_calls) {
  49. // Step 3: call the function
  50. // Note: the JSON response may not always be valid; be sure to handle errors
  51. const availableFunctions = {
  52. get_current_weather: getCurrentWeather,
  53. }; // only one function in this example, but you can have multiple
  54. messages.push(responseMessage); // extend conversation with assistant's reply
  55. for (const toolCall of toolCalls) {
  56. const functionName = toolCall.function.name;
  57. const functionToCall = availableFunctions[functionName];
  58. const functionArgs = JSON.parse(toolCall.function.arguments);
  59. const functionResponse = functionToCall(
  60. functionArgs.location,
  61. functionArgs.unit
  62. );
  63. messages.push({
  64. tool_call_id: toolCall.id,
  65. role: "tool",
  66. name: functionName,
  67. content: functionResponse,
  68. }); // extend conversation with function response
  69. }
  70. const secondResponse = await openai.chat.completions.create({
  71. model: "gpt-3.5-turbo-1106",
  72. messages: messages,
  73. }); // get a new response from the model where it can see the function response
  74. return secondResponse.choices;
  75. }
  76. }
  77. runConversation().then(console.log).catch(console.error);

 一次查询两个城市的天气

 

一次查询两个人的新闻

参考链接:

https://chat.xutongbao.top/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/127315?site
推荐阅读
相关标签
  

闽ICP备14008679号