×

测试deepseek的代码能力

Falcon 2024-06-25 views:
摘要

正在生成中……

我发现deepseek的代码能力真的还可以。

语言转换:

将这个python脚本转换成php脚本

#!/usr/bin/env python3.12

import os
import sys
import argparse
from openai import OpenAI

# 获取环境变量的值
api_key = os.getenv('DEEPSEEK_API_KEY')

# 检查环境变量是否存在
if api_key is None:
    print("Error: The environment variable 'DEEPSEEK_API_KEY' is not set.")
    sys.exit(1)  # 非零退出状态表示错误



client = OpenAI(api_key=api_key, base_url="https://api.deepseek.com")

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "请用简洁的语言描述一下这个网页的内容:https://github.com/jart/cosmopolitan/releases/tag/3.5.0"},
    ],
    stream=False
)

print(response.choices[0].message.content)

DeepSeek-Coder-V2 回复 :

<?php

// 获取环境变量的值
$api_key = getenv('DEEPSEEK_API_KEY');

// 检查环境变量是否存在
if ($api_key === false) {
    echo "Error: The environment variable 'DEEPSEEK_API_KEY' is not set.\n";
    exit(1); // 非零退出状态表示错误
}

$client = new \GuzzleHttp\Client([
    'base_uri' => 'https://api.deepseek.com',
    'headers' => [
        'Authorization' => 'Bearer ' . $api_key,
        'Content-Type' => 'application/json',
    ],
]);

$response = $client->post('/v1/chat/completions', [
    'json' => [
        'model' => 'deepseek-chat',
        'messages' => [
            ['role' => 'system', 'content' => 'You are a helpful assistant'],
            ['role' => 'user', 'content' => '请用简洁的语言描述一下这个网页的内容:https://github.com/jart/cosmopolitan/releases/tag/3.5.0'],
        ],
        'stream' => false,
    ],
]);

$body = json_decode($response->getBody(), true);
echo $body['choices'][0]['message']['content'] . "\n";

?>

修改需求

修改这个php脚本,将此处的 content的值作为变量$contentRawArr元素的值

            ['role' => 'user', 'content' => '请用简洁的语言描述一下这个网页的内容:https://github.com/jart/cosmopolitan/releases/tag/3.5.0'],

比如 $contentRawArr=["content1","content2","content3"];

GuzzleHttp\Client 使用并发请求,并发数为3,将请求后得到的值放入新数组 $contentTranslatedArr

DeepSeek-Coder-V2 回复 :

好的,我将修改 PHP 脚本以实现并发请求,并将请求后得到的值放入新数组 $contentTranslatedArr。以下是修改后的代码:

<?php

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;

// 获取环境变量的值
$api_key = getenv('DEEPSEEK_API_KEY');

// 检查环境变量是否存在
if ($api_key === false) {
    echo "Error: The environment variable 'DEEPSEEK_API_KEY' is not set.\n";
    exit(1); // 非零退出状态表示错误
}

$client = new Client([
    'base_uri' => 'https://api.deepseek.com',
    'headers' => [
        'Authorization' => 'Bearer ' . $api_key,
        'Content-Type' => 'application/json',
    ],
]);

$contentRawArr = [
    "请用简洁的语言描述一下这个网页的内容:https://github.com/jart/cosmopolitan/releases/tag/3.5.0",
    "请用简洁的语言描述一下这个网页的内容:https://github.com/jart/cosmopolitan/releases/tag/3.5.1",
    "请用简洁的语言描述一下这个网页的内容:https://github.com/jart/cosmopolitan/releases/tag/3.5.2"
];

$requests = function () use ($contentRawArr) {
    foreach ($contentRawArr as $content) {
        yield new Request('POST', '/v1/chat/completions', [
            'Content-Type' => 'application/json',
        ], json_encode([
            'model' => 'deepseek-chat',
            'messages' => [
                ['role' => 'system', 'content' => 'You are a helpful assistant'],
                ['role' => 'user', 'content' => $content],
            ],
            'stream' => false,
        ]));
    }
};

$pool = new Pool($client, $requests(), [
    'concurrency' => 3,
    'fulfilled' => function ($response, $index) use (&$contentTranslatedArr) {
        $body = json_decode($response->getBody(), true);
        $contentTranslatedArr[] = $body['choices'][0]['message']['content'];
    },
    'rejected' => function ($reason, $index) {
        echo "Request $index failed: " . $reason->getMessage() . "\n";
    },
]);

$promise = $pool->promise();
$promise->wait();

print_r($contentTranslatedArr);

?>

在这个脚本中,我们使用了 GuzzleHttp 的 Pool 类来实现并发请求。并发数设置为 3,并且我们将每个请求的结果放入 $contentTranslatedArr 数组中。最后,我们打印出 $contentTranslatedArr 数组的内容。

请确保在运行此脚本之前安装了 Guzzle HTTP 客户端库:

composer require guzzlehttp/guzzle
本文收录于