outputs not stop

#6
by bigfacemiao - opened

Download CodeLlama-7b-hf model and test the results of the given example as follows:

sequences = pipeline(
'import socket\n\ndef ping_exponential_backoff(host: str):',
do_sample=True,
top_k=10,
temperature=0.1,
top_p=0.95,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id,
max_length=2000,
)

But the outputs of the model can not stop.

import socket

def ping_exponential_backoff(host: str):
"""
Ping a host with exponential backoff.
:param host: The host to ping.
:return: True if the host is reachable, False otherwise.
"""
# Setup the parameters for the exponential backoff algorithm.
max_attempts = 10
initial_delay = 1
delay_multiplier = 2

# Try to connect to the host.
for attempt in range(max_attempts):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(5)
        s.connect((host, 80))
        return True
    except Exception as e:
        print("Attempt {}: {}".format(attempt + 1, e))
        time.sleep(initial_delay * (delay_multiplier ** attempt))

return False

if name == "main":
host = sys.argv[1]
if ping_exponential_backoff(host):
print("Host {} is up.".format(host))
else:
print("Host {} is down.".format(host))

3.4.2. 异步的重试机制

在上一节中,我们使用了一个简单的算法来实现重试。这种方式是非常有效的,但是如果你想要更多的控制,那么你可以使用异步的重试机制。

import asyncio
from typing import Optional

async def retry(func, *args, **kwargs):
    """
    Retry an asynchronous function until it succeeds.
    :param func: The function to call.
    :param args: The positional arguments to pass to the function.
    :param kwargs: The keyword arguments to pass to the function.
    :return: The result of the function.
    """
    while True:
        try:
            return await func(*args, **kwargs)
        except Exception as e:
            print("Exception: {}".format(e))

async def main():
    async def fetch(url):
        """
        Fetch a URL.
        :param url: The URL to fetch.
        :return: The response from the server.
        """
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                return await response.text()

    text = await retry(fetch, 'https://www.google.com')
    print(text)

asyncio.run(main())

3.4.3. 随机的重试机制

在上一节中,我们使用了一个简单的算法来实现重试。这种方式是非常有效的,但是如果你想要更多的控制,那么你可以使用随机的重试机制。

import random
import time

def retry(func, *args, **kwargs):
    """
    Retry a synchronous function until it succeeds.
    :param func: The function to call.
    :param args: The positional arguments to pass to the function.
    :param kwargs: The keyword arguments to pass to the function.
    :return: The result of the function.
    """
    while True:
        try:
            return func(*args, **kwargs)
        except Exception as e:
            print("Exception: {}".format(e))
            time.sleep(random.uniform(0, 1))

if __name__ == "__main__":
    def fetch(url):
        """
        Fetch a URL.
        :param url: The URL to fetch.
        :return: The response from the server.
        """
        with urllib.request.urlopen(url) as response:
            return response.read().decode('utf-8')

    text = retry(fetch, 'https://www.google.com')
    print(text)

Sign up or log in to comment