chuhades

chuhades

Talk is cheap, show me the shell.
V2EX 第 39699 号会员,加入于 2013-05-26 10:24:06 +08:00
今日活跃度排名 10727
6 G 9 S 73 B
根据 chuhades 的设置,主题列表被隐藏
二手交易 相关的信息,包括已关闭的交易,不会被隐藏
chuhades 最近回复了
9 天前
回复了 Alicewish 创建的主题 iPhone 苹果客服真的需要提升自己的知识水平
你也说了呀,他是 *客服* 而已。
20 天前
回复了 0x19921213 创建的主题 程序员 2023 五月笔记本推荐
@icenine 无所谓了,感觉好看
20 天前
回复了 0x19921213 创建的主题 程序员 2023 五月笔记本推荐
在等新款的雷蛇灵刃 14
Objective-C 能够实现流式接收和逐字输出的效果。你可以使用 NSURLSession 来实现这一功能。以下是一个简单的示例,展示了如何使用 Objective-C 流式接收数据并逐字输出:

```
#import <Foundation/Foundation.h>

@interface StreamDemo : NSObject <NSURLSessionDataDelegate>
@property (nonatomic, strong) NSMutableData *receivedData;
@end

@implementation StreamDemo

- (instancetype)init {
self = [super init];
if (self) {
_receivedData = [[NSMutableData alloc] init];
}
return self;
}

- (void)startStreamingRequest {
NSString *urlString = @"https://api.openai.com/v1/chat/completions?stream=true";
NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];

// 设置请求头信息
[request setValue:@"your-api-key" forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

// 设置请求体信息
NSDictionary *body = @{@"model": @"text-davinci-002",
@"prompt": @"Your prompt here",
@"max_tokens": @30};
NSError *error;
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
if (error) {
NSLog(@"Error creating request body: %@", error);
return;
}
[request setHTTPBody:bodyData];

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
[dataTask resume];
}

#pragma mark - NSURLSessionDataDelegate

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
[self.receivedData appendData:data];

// 在这里实现逐字输出的逻辑
NSString *receivedString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
NSLog(@"Received Data: %@", receivedString);
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Request completed successfully");
}
}

@end

int main(int argc, const char * argv[]) {
@autoreleasepool {
StreamDemo *streamDemo = [[StreamDemo alloc] init];
[streamDemo startStreamingRequest];

// 等待请求完成
[[NSRunLoop currentRunLoop] run];
}
return 0;
}
```

请注意,这个示例仅用于演示目的,你需要根据你的需求对其进行调整。在实际应用中,你可能需要将这个代码与你的 iOS 项目结合起来,实现逐字输出到前端界面。
组成分母
这个女人叫小美,这个男人叫永强
man getloadavg
好玩的东西那么多,为啥要局限于某一个点
关于   ·   帮助文档   ·   博客   ·   nftychat   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2783 人在线   最高记录 5634   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 17ms · UTC 14:26 · PVG 22:26 · LAX 07:26 · JFK 10:26
Developed with CodeLauncher
♥ Do have faith in what you're doing.