OpenAI_API聊天返回null

huangapple go评论77阅读模式
英文:

OpenAI_API chat returning null

问题

I'm attempting to stream the results from a call to ChatGPT using the OpenAI_API library.

public async IAsyncEnumerable<string> Stream(string prompt) {
    var chat = api.Chat.CreateConversation(new ChatRequest() {
        Model = this.LanguageModel,
        Temperature = 0.1,
        MaxTokens = 50
    });
    chat.AppendUserInput(prompt);
    Console.WriteLine(String.Format("Querying with prompt: {0}", prompt));

    int i = 0;
    await foreach (var resp in chat.StreamResponseEnumerableFromChatbotAsync()) {
        Console.WriteLine(i++);
        Console.WriteLine(chat.MostResentAPIResult);
        yield return resp;
    }
    Console.WriteLine("Last");
    Console.WriteLine(chat.MostResentAPIResult);
}

The documentation shows this usage:

var chat = api.Chat.CreateConversation();
chat.AppendUserInput("How to make a hamburger?");

await foreach (var res in chat.StreamResponseEnumerableFromChatbotAsync())
{
	Console.Write(res);
}

Unfortunately, when I test my implementation (without all the console logging) it returns an empty string.

public async Task IntegrationTestPromptStream() {
    IConversation conv = new OpenAIConversation(OpenAI_API.Models.Model.GPT4);
    var response = conv.Stream("Repeat back this sentence \"This is a multi word example that should chunk\"");
    var chunks = new List<string>();
    await foreach (var chunk in response) {
        chunks.Append(chunk);
    }

    Assert.That(
        String.Join(" ", chunks),
        Is.EqualTo("This is a multi word example that should chunk"),
        "Didn't receive expected response from OpenAI chat");
}

I added the console logging statements to work out what was being invoked, and this is the output:

Querying with prompt: Repeat back this sentence "This is a multi word example that should chunk"
0

NUnit Adapter 4.1.0.0: Test execution complete

If I attach the debugger and attempt to examine chat.MostResentAPIResult, I can see an object, but notably the .ToString() method throws a null reference exception.

OpenAI_API聊天返回null

Finally, if I just keep the index counter in but remove the statements to output the last response, I get this:

Querying with prompt: Repeat back this sentence "This is a multi word example that should chunk"
0
1
2
3
4
5
6
7
8
9
Last

So it'll iterate through empty strings all day long, but attempting to look at the response returns a null or a NullReferenceException.

What am I missing?

英文:

I'm attempting to stream the results from a call to ChatGPT using the OpenAI_API library.

public async IAsyncEnumerable&lt;string&gt; Stream(string prompt) {
    var chat = api.Chat.CreateConversation(new ChatRequest() {
        Model = this.LanguageModel,
        Temperature = 0.1,
        MaxTokens = 50
    });
    chat.AppendUserInput(prompt);
    Console.WriteLine(String.Format(&quot;Querying with prompt: {0}&quot;, prompt));

    int i = 0;
    await foreach (var resp in chat.StreamResponseEnumerableFromChatbotAsync()) {
        Console.WriteLine(i++);
        Console.WriteLine(chat.MostResentAPIResult);
        yield return resp;
    }
    Console.WriteLine(&quot;Last&quot;);
    Console.WriteLine(chat.MostResentAPIResult);
}

The documentation shows this usage:

var chat = api.Chat.CreateConversation();
chat.AppendUserInput(&quot;How to make a hamburger?&quot;);

await foreach (var res in chat.StreamResponseEnumerableFromChatbotAsync())
{
	Console.Write(res);
}

Unfortunately, when I test my implementation (without all the console logging) it returns an empty string.

public async Task IntegrationTestPromptStream() {
    IConversation conv = new OpenAIConversation(OpenAI_API.Models.Model.GPT4);
    var response = conv.Stream(&quot;Repeat back this sentence \&quot;This is a multi word example that should chunk\&quot;&quot;);
    var chunks = new List&lt;string&gt;();
    await foreach (var chunk in response) {
        chunks.Append(chunk);
    }

    Assert.That(
        String.Join(&quot; &quot;, chunks),
        Is.EqualTo(&quot;This is a multi word example that should chunk&quot;),
        &quot;Didn&#39;t receive expected response from OpenAI chat&quot;);
}

I added the console logging statements to work out what was being invoked and this is the output

Querying with prompt: Repeat back this sentence &quot;This is a multi word example that should chunk&quot;
0

NUnit Adapter 4.1.0.0: Test execution complete

If I attach the debugger and attempt to examine chat.MostResentAPIResult [sic], I can see an object, but notably the .ToString() method throws a null reference exception.

OpenAI_API聊天返回null

Finally, if I just keep hte index counter in but remove the statements to output the last response, I get this

Querying with prompt: Repeat back this sentence &quot;This is a multi word example that should chunk&quot;
0
1
2
3
4
5
6
7
8
9
Last

So It'll iterate through empty strings all day long but attempting to look at the response returns a null or a NullReferenceException.

What am I missing?

答案1

得分: 1

这段代码中,OutputAppend() 只会显示从 OutputAdd() 中添加的内容。而当运行 OutputAppendFixed() 时,它将使用 Append 方法添加。这涉及到 Append 返回一个 IEnumerable&lt;T&gt; 的原因。

Add 方法修改原始数据,而 Append 方法返回一个不修改原始数据的新数组。

英文:

Example code

using System.Collections;
using System.Collections.Generic;


List&lt;string&gt; strings = new List&lt;string&gt;();

void OutputAdd()
{
    Console.WriteLine(&quot;Output for Add Mehtod ...&quot;);


    for (int i = 0; i &lt; 10; i++)
    {
        strings.Add($&quot;Testing using Add {i}&quot;);
    }

    foreach (var line in strings)
    {
        Console.WriteLine(line);
    }
}

void OutputAppend()
{
    Console.WriteLine(&quot;Output for Append Mehtod ...&quot;);


    for (int i = 0; i &lt; 10; i++)
    {
        strings.Append($&quot;Testing using Append {i}&quot;);
    }

    foreach (var line in strings)
    {
        Console.WriteLine(line);
    }
}

void OutputAppendFixed()
{
    Console.WriteLine(&quot;Output for Append Fixed Mehtod ...&quot;);


    for (int i = 0; i &lt; 10; i++)
    {
        strings = strings.Append($&quot;Testing using Append with added code {i}&quot;).ToList();
    }

    foreach (var line in strings)
    {
        Console.WriteLine(line);
    }
}

OutputAdd();

OutputAppend();

OutputAppendFixed();

If you run this, you'll see that OutputAppend() will only show what was added from OutputAdd(). Then when OutputAppendFixed() runs it will add using the Append method. This has to do with Append returning an IEnumerable&lt;T&gt;.

The Add modifies the original data while Append returns a new array without modifying the original data.

huangapple
  • 本文由 发表于 2023年5月6日 20:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定