labring / FastGPT

FastGPT is a knowledge-based platform built on the LLMs, offers a comprehensive suite of out-of-the-box capabilities such as data processing, RAG retrieval, and visual AI workflow orchestration, letting you easily develop and deploy complex question-answering systems without the need for extensive setup or configuration.
https://tryfastgpt.ai
Other
17.78k stars 4.74k forks source link

ios如何请求api接口 #880

Closed wc2411888 closed 1 month ago

wc2411888 commented 8 months ago

我的ios使用了什么方式都无法正确调用api接口,请问有参考代码吗?用postman可以访问,但iOS不知道怎么去流式请求

wc2411888 commented 8 months ago

import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://wecom-ai-test.integrity.com.cn:5002/api/v1/chat/completions"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0]; NSDictionary headers = @{ @"Authorization": @"Bearer fastgpt-xx", @"Content-Type": @"application/json" };

[request setAllHTTPHeaderFields:headers]; NSData *postData = [[NSData alloc] initWithData:[@"{\r\n \"chatId\": \"abcd\",\r\n \"stream\": true,\r\n \"detail\": false,\r\n \"variables\": {\r\n \"uid\": \"asdfadsfasfd2323\",\r\n \"name\": \"张三\"\r\n },\r\n \"messages\": [\r\n {\r\n \"content\": \"你是什么大模型\",\r\n \"role\": \"user\"\r\n }\r\n ]\r\n}" dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession session = [NSURLSession sharedSession]; NSURLSessionDataTask dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData data, NSURLResponse response, NSError error) { if (error) { NSLog(@"%@", error); dispatch_semaphore_signal(sema); } else { NSHTTPURLResponse httpResponse = (NSHTTPURLResponse ) response; NSError parseError = nil; NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError]; NSLog(@"%@",responseDictionary); dispatch_semaphore_signal(sema); } }]; [dataTask resume]; dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

wc2411888 commented 8 months ago

stream 设置为false可以请求到结果 ,stream 设置为true就会失败

c121914yu commented 8 months ago

没开发过IOS,蹲一手~