chuhades

chuhades

Talk is cheap, show me the shell.
V2EX 第 39699 号会员,加入于 2013-05-26 10:24:06 +08:00
今日活跃度排名 14962
6 G 42 S 57 B
根据 chuhades 的设置,主题列表被隐藏
二手交易 相关的信息,包括已关闭的交易,不会被隐藏
chuhades 最近回复了
21 天前
回复了 linuxgo 创建的主题 Linux Linux 下有没有可以远程连接 win7 桌面的软件
remmina
找检测公司;找除醛公司
62 天前
回复了 sadyx 创建的主题 程序员 前端代码缩进, 2 格还是 4 格?
统一即美
69 天前
回复了 bluehtt 创建的主题 程序员 求程序员笔记本
雷蛇 14 寸,mint ,目前体验还行。

缺点:重、待机时间和之前的 mbp m1 比差一些,3 小时左右。
75 天前
回复了 mdgwmt0 创建的主题 Linux Linux ,哪个系统做桌面比较方便
这两天感觉 manjaro gnome 挺舒服的
76 天前
回复了 iqoo 创建的主题 程序员 C++ 可根据参数值进行重载吗?
模板特化
127 天前
回复了 Alicewish 创建的主题 iPhone 苹果客服真的需要提升自己的知识水平
你也说了呀,他是 *客服* 而已。
138 天前
回复了 0x19921213 创建的主题 程序员 2023 五月笔记本推荐
@icenine 无所谓了,感觉好看
139 天前
回复了 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 项目结合起来,实现逐字输出到前端界面。
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1156 人在线   最高记录 6067   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 75ms · UTC 23:12 · PVG 07:12 · LAX 16:12 · JFK 19:12
Developed with CodeLauncher
♥ Do have faith in what you're doing.